iOS: HTTP response and getting content of your file from NSData
I'm a novice iOS developer and I'm trying to add a feature to existing code. I want to upload a pdf file from my machine(PC/Mac) to my iPad/iPhone application, i setup the all socket stuffs and currently I can do the reverse thing(Send a pdf file from my iPhone/iPad to my machine by logging to my iPad's ip address in my machine's web browser and open/download the file there(it was first task, it's done now), now I want to be able to upload a pdf file to my iPad through the same UI in browser, where I have the select/submit button (i added submit/choose file buttons in browser where i log to the ip address of my iPad, I used this code to add them:
[thestring appendFormat:@"<form method=\"post\" enctype=\"multipart/form-data\" />"];
[thestring appendFormat:@"<input name=\"file\" type=\"file\" />"];
[thestring appendFormat:@"<input type=\"submit\" />"];
[thestring appendFormat:@"</form>"];
Now I'm in the point in my code, when I'm getting the response by clicking on the "submit" button to download the selected file to iPad, I have the access to "CFHTTPMessageRef" and from there I can get the NSData but not sure how to parse the data to get the content of pdf file and other info like filename,.. etc from NSData? the data comes in a form of:
Content-Type: multipart/form-data; boundary=---------------------------3154721346826
Content-Length: 226
-----------------------------3154721346826
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
This is my test text file data开发者_C百科 uploaded.
-----------------------------3154721346826--
My question is how I can find the pdf content(in above example :This is my test text file data uploaded) in the NSData? how about when the pdf is a huge file?
Thanks, Kamran
NSData is the file binary data that is sent. Filenames etc. you need to take from the metadata.
As for saving it to disk you can use NSData's writeToFile:atomically: method.
To get a proper path to save to, you can read this post among many others: How can I get a writable path on the iPhone?
Cheers, Oded.
精彩评论