How to download a file (iOS/Cocoa SDK) from URL where file name is not the part of URL

前端 未结 1 1452
慢半拍i
慢半拍i 2021-01-21 03:37

I was wondering on how to download a file using NSUrl where the filename is not available or is not part of the request URL. I have been looking at ASIHTTPRequest (http://allsee

相关标签:
1条回答
  • 2021-01-21 04:17

    In the NSURLConnection delegate callback - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response you can retrieve the suggestedFileName from the NSURLResponse. If you get unknown for a name then you need to come up with another name for your file. At this point you can specify your download path before you start saving data.

    suggestedFilename

    Returns a suggested filename for the response data.

    • (NSString *)suggestedFilename

    Return Value

    A suggested filename for the response data.

    Discussion

    The method tries to create a filename using the following, in order:

    1. A filename specified using the content disposition header.

    2. The last path component of the URL.

    3. The host of the URL.

    If the host of URL can't be converted to a valid filename, the filename “unknown” is used.

    In most cases, this method appends the proper file extension based on the MIME type. This method will always return a valid filename regardless of whether or not the resource is saved to disk.

    Availability

    Available in iOS 2.0 and later. Declared In NSURLResponse.h

    0 讨论(0)
提交回复
热议问题