我有一個UIWebView
從需要身份驗證的Web服務器下載文件(PDF,Word,Excel)。爲此,我已經處理了身份驗證並根據Apple建議填充了一個NSMutableData
對象。UIWebView和文件MIME類型
將數據加載到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);
}
響應頭的位置:
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
編輯: 一個奇怪的現象會發生的事情與標題。
我最初發布在Chrome和Fiddler2進行了覈實,但NSURLConnection的實際接收的標題:
「的Content-Type」= 「text/html的;字符集= US-ASCII,應用程序/ 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]);
}
}
任何想法,爲什麼它接收到這個無效的頭,我難倒?
好像它應該工作,但是這是行不通的:NSString的mime類型= [[類HTTPResponse allHeaderFields] objectForKey:@ 「內涵式」] stringValue的]。 – ServerMonkey 2013-02-28 23:36:43
那麼你應該得到重定向,我想。請檢查您的** - (NSURLRequest *)連接中的響應:(NSURLConnection *)連接willSendRequest:(NSURLRequest *)請求redirectResponse:(NSURLResponse *)響應**方法。 didReceiveResponse僅在有服務器發送響應時調用,其他委託方法負責程序流。 – CodeWeed 2013-03-01 00:33:36