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://allseeing-i.com/ASIHTTPRequest/) library. It has
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.txt"];
But the point is you need to know the file name here. For example if you put the URL (not real URL) http://some-website/foo.pdf
It downloads a file foo.pdf and I want to save it like that. Any ideas thanks.
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:
A filename specified using the content
disposition header.
The last path
component of the URL.
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