UrlDownloadToFile 下载不了中文名文件

landixin 2011-02-23 03:11:25
通过一个html页面,调用activex。html页面的charset=gb2312,运行时需要先下载图片到某个盘的文件夹下,现在由于有中文名图片,下载不成功。
function DownloadFile(Source, Dest: string): Boolean;
begin
try

Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
except
Result := False;
end;
end;
以上是下载写的方法。后来找了点资料改成下面的代码
function DownloadFile(Source, Dest: string): Boolean;
begin
try

Result := UrlDownloadToFile(nil, PChar(urlencode(utf8encode(source))), PChar(urlencode(utf8encode(Dest))), 0, nil) = 0;
except
Result := False;
end;
end;
还是不行。请大家指教一下有什么好的办法。另说明:应要求中文名文件不能改为英文名
...全文
487 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Forever_Young 2011-02-24
  • 打赏
  • 举报
回复
呵呵,楼主都被回帖转晕了,代码页和编码转换其实很简单,但是一般都自己被自己转晕了,给你做了个例子,应该是你要的。

uses
HTTPApp, IdURI

TIdURI.URLEncode('http://192.168.0.2/从未试过拥有.APE')
结果:http://192.168.0.2/%E4%BB%8E%E6%9C%AA%E8%AF%95%E8%BF%87%E6%8B%A5%E6%9C%89.APE

UTF8ToString(HTTPDecode('http://192.168.0.2/%E4%BB%8E%E6%9C%AA%E8%AF%95%E8%BF%87%E6%8B%A5%E6%9C%89.APE'))
结果: http://192.168.0.2/从未试过拥有.APE
landixin 2011-02-24
  • 打赏
  • 举报
回复
谢谢大家的帮忙。我已经用最简单的办法解决了。还是用这个办法UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
。只不过我在服务端上方的文件名都改成英文名,下载保存到自己电脑上用中文名表示就可以了。
国风 2011-02-23
  • 打赏
  • 举报
回复
URL 用 AnsiToUtf8
返回的html 用 Utf8ToAnsi
landixin 2011-02-23
  • 打赏
  • 举报
回复
widows xp ,delphi7
kye_jufei 2011-02-23
  • 打赏
  • 举报
回复
你是什麼環境?。。。
landixin 2011-02-23
  • 打赏
  • 举报
回复
谢谢,试过把utf8encode換成AnsiToUTF8、Gb2312ToUnicode还是不行
kye_jufei 2011-02-23
  • 打赏
  • 举报
回复
那麼你再把utf8encode換成AnsiToUTF8、Gb2312ToUnicode。。。試試看
landixin 2011-02-23
  • 打赏
  • 举报
回复
试用UTF8ToAnsi不行
mjp1234airen4385 2011-02-23
  • 打赏
  • 举报
回复
c,写反了,是AnsiToUTF8
kye_jufei 2011-02-23
  • 打赏
  • 举报
回复
直接引用Uses URLMon,不要寫那個function,它本身就有這個方法。然後直接pchar(urlencode(utf8encode(strDownloadFile)))即可。。。
mjp1234airen4385 2011-02-23
  • 打赏
  • 举报
回复
试试UTF8ToAnsi
landixin 2011-02-23
  • 打赏
  • 举报
回复
我不是后来改成
UrlDownloadToFile(nil, PChar(urlencode(utf8encode(source))), PChar(urlencode(utf8encode(Dest))), 0, nil) = 0;
还是不行吗?
kye_jufei 2011-02-23
  • 打赏
  • 举报
回复
這樣就可以了,pchar(urlencode(utf8encode(strDownloadFile)))。。。
kye_jufei 2011-02-23
  • 打赏
  • 举报
回复
你原来的代码。。。

我的哥呀,你转换一下编码不行吗?...

landixin 2011-02-23
  • 打赏
  • 举报
回复
我下载英文名文件是可以的,中文名文件就失败了。楼上的代码就是我原来的的代码。有没高手指教一下
kye_jufei 2011-02-23
  • 打赏
  • 举报
回复
Uses URLMon, ShellApi;
function DownloadFile(SourceFile, DestFile: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
except
Result := False;
end;
end;

procedure TForm1.Button1.Click(Sender: TObject);
const
// URL Location
SourceFile := http://www.google.com/intl/de/images/home_title.gif;
// Where to save the file
DestFile := c:\temp\google-image.gif;
begin
if DownloadFile(SourceFile, DestFile) then
begin
ShowMessage(Download succesful!);
// Show downloaded image in your browser
ShellExecute(Application.Handle,PChar(open),PChar(DestFile),PChar(),nil,SW_NORMAL)
end
else
ShowMessage(Error while downloading + SourceFile)
end;

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧