IOS File Download (afnetwork three-party framework file name in the text characters garbled problem solved) four

Source: Internet
Author: User

A long time ago, the andriod part of this article and the server part, the recent paper on the way, so wrote a few lines of simple code as follows.

Take the afnetwork for a moment. The specific code is as follows:

-(void) download{nsurlsessionconfiguration *config = [Nsurlsessionconfiguration defaultsessionconfiguration];        Afurlsessionmanager *manager = [[Afurlsessionmanager alloc] initwithsessionconfiguration:config];              NSString *urlstring = @ "Http:192.168.0.179:8080/myweb/download.do";        URLString = [URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];    Nsurl *url = [Nsurl urlwithstring:urlstring];            Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; Nsurlsessiondownloadtask *task = [Manager downloadtaskwithrequest:request progress:nil Destination:^nsurl * (NSURL * TargetPath, Nsurlresponse *response) {//Specify the path saved by the download file//NSLog (@ "%@%@", TargetPath, Response.suggestedfilena        ME); Save the download file in the cache path NSString *cachedir = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES)                [0]; NSString *retstr = [Response.suggestedfilename stringbyreplacingpercentescapesusingencoding:nSutf8stringencoding];                NSLog (@ "%@ \ n", retstr);        NSString *path = [Cachedir stringbyappendingpathcomponent:retstr];                                Nsurl *fileurl = [Nsurl Fileurlwithpath:path];    return FileURL;     } completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {NSLog (@ "%@%@", FilePath, error);        }]; [Task resume];}
1. Create a request using Nsurlrequest.

2. Use

Nsurlsessiondownloadtask
To create a task.

3. File name you want to use, name on the server, call

Response.suggestedfilename
Can

4. The file name contains Chinese garbled characters, the server side of the last code needs to be modified as follows, using Urlencoder:

Modify http://blog.csdn.net/nothingl3/article/details/44409319 (has been modified)

String Filename=file.getname ();
Note that the Urlencoder encode method is used to encode the file name Utf-8
String enfilename = Urlencoder.encode (FileName, "utf-8");
System.out.println (Enfilename);
Browser download
Response.AddHeader ("Content-disposition", "attachment;filename=" + enfilename);

5.iOS just need to encode the URL---"Utf-8:

NSString *retstr = [Response.suggestedfilename stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];

IOS File Download (afnetwork three-party framework file name in the text characters garbled problem solved) four

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.