After the new year to continue learning, let us learn how to calculate the download file size, and how to post upload files

Source: Internet
Author: User
Tags vars

Today is only second day, yesterday is home so long since the only let go of a good fun day, play very happy. After the happy and began to continue to study and review. First of all, may the new Year all the best, the most important health

First, let's talk about how to get information about downloading resources.

NSString *url_string = @ "Http://box.dwstatic.com/skin/Teemo/Teemo_Splash_0.jpg";

Nsmutableurlrequest *request =[nsmutableurlrequest requestwithurl:[nsurl urlwithstring:url_string] CachePolicy: Nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];

Request. HttpMethod = @ "HEAD";

[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

NSLog (@ "%@", response);

NSLog (@ "---------------");

NSLog (@ "%@", data);

}

This method only obtains information about the downloaded resource and does not return the data body.

Besides this there are other ways to get the size

Nsurl *url = [nsurl urlwithstring:url_string];//turn into URL

Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy: Nsurlrequestuseprotocolcachepolicy TIMEOUTINTERVAL:30.F]; Create a variable network request

[Request sethttpmethod:@ "GET"];

[Nsurlconnection connectionwithrequest:request delegate:self];

Implementing a Proxy

-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response

{

NSLog (@ "To download a file size of%@", response);

}

As to how to download this document, I believe there are many ways.

As for file upload.

You can create an upload model first.

  1. #import "UploadFile.h"
  2. @implementation UploadFile
  3. Stitching strings
  4. Static NSString *boundarystr = @ "--"; //delimited string
  5. static NSString *randomidstr; //This upload flag string
  6. static NSString *uploadid; //Upload (PHP) script, receive file fields
  7. -(Instancetype) init
  8. {
  9. Self = [super Init];
  10. if (self) {
  11. Randomidstr = @ "Itcast";
  12. Uploadid = @ "UploadFile";
  13. }
  14. return self ;
  15. }
  16. #pragma mark-Private method
  17. -(NSString *) Topstringwithmimetype: (nsstring *) MimeType uploadfile: (nsstring *) UploadFile
  18. {
  19. nsmutablestring *STRM = [nsmutablestring string];
  20. [StrM AppendFormat:@ "%@%@\n", Boundarystr, randomidstr];
  21. [StrM AppendFormat:@ "content-disposition:form-data; name=\"%@\ "; filename=\"%@\ "\ n", Uploadid,   UploadFile];
  22. [StrM AppendFormat:@ "Content-type:%@\n\n", MimeType];
  23. NSLog (@ "%@", StrM);
  24. return [StrM copy];
  25. }
  26. -(NSString *) bottomstring
  27. {
  28. nsmutablestring *STRM = [nsmutablestring string];
  29. [StrM AppendFormat:@ "%@%@\n", Boundarystr, randomidstr];
  30. [StrM appendString:@ "content-disposition:form-data; name=\" submit\ "\ n"];
  31. [StrM appendString:@ "submit\n"];
  32. [StrM AppendFormat:@ "%@%@--\n", Boundarystr, randomidstr];
  33. NSLog (@ "%@", StrM);
  34. return [StrM copy];
  35. }
  36. #pragma mark-Upload file
  37. -(void) Uploadfilewithurl: (nsurl *) URL data: (NSData *) data
  38. {
  39. //1> Data Body
  40. nsstring *topstr = [self topstringwithmimetype:@ "Image/png" uploadfile:@ "Avatar 1.png"];
  41. nsstring *bottomstr = [self bottomstring];
  42. nsmutabledata *datam = [nsmutabledata data];
  43. [Datam appenddata:[topstr datausingencoding:nsutf8StringEncoding];
  44. [Datam Appenddata:data];
  45. [Datam appenddata:[bottomstr datausingencoding:nsutf8StringEncoding];
  46. //1. Request
  47. nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:0   timeOutInterval:2. 0f];
  48. //Datam out of Scope will be released, so no copy
  49. Request.  Httpbody = Datam;
  50. //2> Set the header property of the request
  51. Request.  HttpMethod = @ "POST";
  52. //3> settings Content-length
  53. nsstring *strlength = [NSString stringwithformat:@ "%ld", (long) Datam. length];
  54. [Request Setvalue:strlength Forhttpheaderfield:@ "Content-length"];
  55. //4> settings Content-type
  56. nsstring *strcontenttype = [NSString stringwithformat:@ "multipart/form-data; boundary=%@",   RANDOMIDSTR];
  57. [Request Setvalue:strcontenttype Forhttpheaderfield:@ "Content-type"];
  58. //3> connection server send request
  59. [Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue alloc] init] completionhandler:^ (nsurlresponse *response, nsdata *data, nserror *connectionerror) {
  60. nsstring *result = [[NSString alloc] initwithdata:data encoding:nsutf8StringEncoding];
  61. NSLog (@ "%@", result);
  62. }];
  63. }
  64. @end

Then directly in the controller call-(void) Uploadfilewithurl: (nsurl *) URL data: (NSData *) data.

About the upload of code from http://blog.csdn.net/codywangziham01/article/details/38044637, this blog about the download and upload of the document is a lot of detailed explanation.

After the new year to continue learning, let us learn how to calculate the download file size, and how to post upload files

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.