更改Cocoahttpserver中的下载文件名 [英] Change Download File Name in Cocoahttpserver

查看:118
本文介绍了更改Cocoahttpserver中的下载文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iphone应用程序中使用cocoahttpserver,但是当我尝试下载它时(通过单击标准演示应用程序中的链接),我的sqlite文件(myDatabase.sqlite)以未知的形式到达我的Mac桌面完全没有后缀。但是,当我另存为...时,它提供的名称很好。我希望它用sqlite后缀保存文件。

I'm using cocoahttpserver in an iphone app, but when I try to download it (by clicking a link in the standard demo app), my sqlite file (myDatabase.sqlite) arrives on my Mac Desktop as "Unknown" with no suffix at all. However, when I "Save As..." it provides the name fine. I would prefer it to save the file with the sqlite suffix.

所以,它必须是导致问题的后缀????

So, it must be the suffix causing the problems????

如果这是问题,我似乎无法在类中找到下载正确文件名的方法但是在将其呈现给浏览器时更改它(带有.backup或.db等后缀) ,或者有效的东西)。

If this is the problem, I cannot seem to find a way in the classes to download the correct file name BUT then change it when presenting it to the browser (with a suffix like .backup, or .db, or something that works).

任何人都知道在哪些类中更改下载文件名,以便浏览器(Safari)不会将其称为未知?谢谢。

Anyone know where in the classes to change the download file name so the browser (Safari) will not call it "unknown"? Thanks.

推荐答案

我找到了其他人的代码(MyAppSales),在replyToHTTPRequest中,我添加了Content-Disposition标头如下(in方法的一部分),现在它可以工作!

I found someone else's code (MyAppSales) and in replyToHTTPRequest, I added the Content-Disposition header as below (in one section of the method), and now it works!

if(!isRangeRequest)
        {
            // Status Code 200 - OK
            response = CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1);

            NSString *contentLengthStr = [NSString stringWithFormat:@"%qu", contentLength];
            CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Length"), (CFStringRef)contentLengthStr);

            // ************* added this from MyAppSales
            if ([httpResponse isKindOfClass:[HTTPFileResponse class]])
            {
                NSString *baseName = [(NSString *)[(HTTPFileResponse *)httpResponse filePath] lastPathComponent];

                NSString *contentDispoStr = [NSString stringWithFormat:@"Content-Disposition: attachment; filename=\"%@\"", baseName];
                CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Disposition"), (CFStringRef)contentDispoStr);
            }

        }

这篇关于更改Cocoahttpserver中的下载文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆