【问题标题】:download data error when using ASIHTTPRequest to set the download destination file path in requestDidReceiveResponseHeadersSelector: method使用 ASIHTTPRequest 在 requestDidReceiveResponseHeadersSelector: 方法中设置下载目标文件路径时下载数据错误
【发布时间】:2012-06-18 19:29:03
【问题描述】:

我使用 ASIHTTPRequest 将文件下载到 ios 设备 当我在创建请求后立即设置下载目标文件路径时,它运行良好,我的代码是: 一个

SIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:@"/Users/zxllf23/Desktop/download/tmp.zip"]];
    [request setTemporaryFileDownloadPath:@"/Users/zxllf23/Desktop/download/tmp.zip.download"]];

但这不舒服,服务器上的 bcs 不同文件有不同的文件名,我们可以从 http 响应头的 Content-Disposition 中检索,所以我想自动确定文件名,我在 requestDidReceiveResponseHeadersSelector: 方法中编写了我的代码

-(void) downloadReceiveRespone:(ASIHTTPRequest *)request
{
    NSString *CoententDecription = [[request responseHeaders] objectForKey:@"Content-Disposition"];
    NSString *filename = [self getFilenameFrom:CoententDecription];

    [request setDownloadDestinationPath:[NSString stringWithFormat:@"/Users/zxllf23/Desktop/download/%@",filename]];
    [request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"/Users/zxllf23/Desktop/download/%@.download",filename]];

}

哪里可以成功下载文件,但是无法正常打开,我对比下载的文件数据和原文件,不一样

任何人都可以在这个问题上帮助我,我将非常感激。

【问题讨论】:

    标签: iphone asihttprequest filenames content-disposition


    【解决方案1】:

    不支持在请求开始后设置 setDownloadDestinationPath,这可以解释为什么它不能正常工作。

    请求完成后移动文件,或者自己将数据写入文件。

    【讨论】:

    • 感谢 JosephH,但是这样的话,我怎样才能恢复之前中止的下载呢?
    • 如果您需要恢复,我会提出我的第一个建议:在请求完成后移动文件。然后您可以正常恢复,只有在文件完全下载成功后才将文件移动到最终位置。
    • 谢谢,但我还是不太明白,能不能详细解释一下,谢谢。
    • 基本上:使用临时文件名下载文件,然后仅在下载完成后将其重命名为最终的正确文件名。 IE。在 requestDidFinish 选择器中,将文件重命名为正确的文件名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2011-03-21
    相关资源
    最近更新 更多