gpt4 book ai didi

ios - UIWebView 和文件 Mime 类型

转载 作者:行者123 更新时间:2023-12-01 16:51:42 24 4
gpt4 key购买 nike

我有一个 UIWebView从需要身份验证的 Web 服务器下载文件(PDF、Word、Excel)。为此,我处理了身份验证并填充了 NSMutableData根据 Apple 的建议反对。

将数据加载到 UIWebView 时我不想对 Mime 类型进行硬编码,关于如何避免这种情况的任何想法?

[_webView loadData:data MimeType: @"application/msword" textEncodingName: @"UTF-8" baseURL:[NSURL URLWithString: @""]];

尝试了以下但总是返回“text/html”:
- (void)connection:(NSURLConnection *)connection didRecieveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSString *mimeType = [httpResponse MIMEType];
NSLog(@"MIMETYPE: %@",mimeType);
}

响应 header 在这里:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 50176
Content-Type: application/msword
Expires: -1
Server: Microsoft-IIS/7.5
Content-Disposition: attachment; filename=test.doc
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET

编辑:
标题发生了一些奇怪的事情。

我最初发布的 header 已在 Chrome 和 Fiddler2 中验证,但 NSURLConnection 实际上正在接收:

"Content-Type" = "text/html;charset=us-ascii,application/msword";
"Content-Length" = "341, 79360";



以下代码段用于确认这一点:
- (void)connection:(NSURLConnection *)connection didRecieveResponse:(NSURLResponse *)response {

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
}
}

知道为什么它会收到这个无效的标题,我很难过?

最佳答案

使用

[[httpReponse allHeaderFields] objectForKey:@"Content-Type"] isEqualToString:@"application/msword"]

检查响应头。

可能是您错过了请求期间发生多次重定向的事实。

关于ios - UIWebView 和文件 Mime 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com