Your SlideShare is downloading. ×
0
AFNetworkingTutor : Michael
AFNetworking• https://github.com/AFNetworking/AFNetworking
Installation
ARC Issue
Points• Upload File• Download File
Server• GET Download File• POST Upload File• Reference http://goo.gl/OGevb
Server - Receive Data by POSTvar express = require(express);var app = express( );app.configure(function() {        app.use...
Server - req.files{ uploadingFile:   { size: 15198,     path: 7fedaa5a4b44a499e2cfd29fc7c3be71,     name: object.png,     t...
Upload Image - Http ClientNSURL *url = [NSURL URLWithString:@"http://192.168.2.104/~chronoer/php_test/Upload.php"];AFHTTPC...
Upload Image - Data & RequestNSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"me.jpg"],0.5);NSMutableUR...
Upload Image - Success & FailAFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request] ;...
Upload Image - Progress[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten,long long t...
DemoFileDownload&Upload/AFNetworkDemo
Server - Send Data by GETapp.get(/data, function(req,res) {" if (req.query.fileName) {" "" " var filename = req.query.file...
Download File - Set up get linkNSURL *url = [NSURL URLWithString:[FILESERVER2 stringByAppendingFormat:@"/%@?fileName=%@",@...
Download File - store file pathNSString *path = @"/Users/chronoer/Desktop/tmp/yen.jpg";operation.outputStream = [NSOutputSt...
Download File - bind with progress view[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long longtotalBytesR...
Demo• FileDownloadUpload
Question ?
Upcoming SlideShare
Loading in...5
×

Afnetworking 26.key

1,128

Published on

http://www.facebook.com/pages/Developers-note/226724001803

Published in: Technology, Art & Photos
2 Comments
1 Like
Statistics
Notes
  • Hi, My Server Side is built by Node.js. It may not help you instantly.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • is it possible for you to send the demo to my email please..?
    and also in the server side I'm trying to write a servlet to get an Image by POST... do you have any suggestion or example for that..?

    My email is farhad.ris@me.com

    TanQ a lot
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,128
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
2
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Afnetworking 26.key"

  1. 1. AFNetworkingTutor : Michael
  2. 2. AFNetworking• https://github.com/AFNetworking/AFNetworking
  3. 3. Installation
  4. 4. ARC Issue
  5. 5. Points• Upload File• Download File
  6. 6. Server• GET Download File• POST Upload File• Reference http://goo.gl/OGevb
  7. 7. Server - Receive Data by POSTvar express = require(express);var app = express( );app.configure(function() {        app.use(express.bodyParser({uploadDir: ./}));});app.listen(8800);var fs = require(fs);app.post(/fileUpload, function(req, res) {    var uploadedFile = req.files.uploadingFile;    var tmpPath = uploadedFile.path;    var targetPath = ./ + uploadedFile.name;    fs.rename(tmpPath, targetPath, function(err) {        if (err) throw err;        fs.unlink(tmpPath, function() {                              console.log(File Uploaded to + targetPath + - + uploadedFile.size + bytes);        });    });    res.send(file upload is done.);    res.end();});<form enctype="multipart/form-data" action="upload.php"method="POST"><input type="hidden" name="MAX_FILE_SIZE" value="100000" />Choose a file to upload: <input name="uploadingfile" type="file" /><br /><input type="submit" value="Upload File" /></form>
  8. 8. Server - req.files{ uploadingFile:   { size: 15198,     path: 7fedaa5a4b44a499e2cfd29fc7c3be71,     name: object.png,     type: image/png,     hash: false,     lastModifiedDate: Mon Aug 27 2012 09:06:45 GMT+0800 (CST),     _writeStream:      { path: 7fedaa5a4b44a499e2cfd29fc7c3be71,        fd: 10,        writable: false,        flags: w,        encoding: binary,        mode: 438,        bytesWritten: 15198,        busy: false,        _queue: [],        _open: [Function],        drainable: true },     length: [Getter],     filename: [Getter],     mime: [Getter]    }}
  9. 9. Upload Image - Http ClientNSURL *url = [NSURL URLWithString:@"http://192.168.2.104/~chronoer/php_test/Upload.php"];AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
  10. 10. Upload Image - Data & RequestNSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"me.jpg"],0.5);NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"path:@"" parameters:nil constructingBodyWithBlock: ^(id<AFMultipartFormData>formData) {[formData appendPartWithFileData:imageData name:@"uploadedfile"fileName:@"yen.jpg" mimeType:@"image/jpeg"];}];<form enctype="multipart/form-data" action="upload.php" method="POST"><input type="hidden" name="MAX_FILE_SIZE" value="100000" />Choose a file to upload: <input name="uploadedfile" type="file" /><br /><input type="submit" value="Upload File" /></form>HTML
  11. 11. Upload Image - Success & FailAFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request] ;[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, idresponseObject) {NSLog(@"ok");} failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"failed");}];[operation start];
  12. 12. Upload Image - Progress[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten,long long totalBytesExpectedToWrite) {NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);progressView.progress = totalBytesWritten/[imageData length];}];
  13. 13. DemoFileDownload&Upload/AFNetworkDemo
  14. 14. Server - Send Data by GETapp.get(/data, function(req,res) {" if (req.query.fileName) {" "" " var filename = req.query.fileName;" console.log(filename);" "" " fs.stat(filename, function(error, stat) {" " if (error) { throw error; }" " res.writeHead(200, {" " Content-Type : image/png," " Content-Length : stat.size" " });" " });" var fileStream = fs.createReadStream(filename);" fileStream.on(data, function (data) {" res.write(data);" });" fileStream.on(end, function() {" res.end();" });" "" }else{" " res.writeHead(404,{"Content-Type": "application/zip"});" " res.write("error");" " res.end();" }});
  15. 15. Download File - Set up get linkNSURL *url = [NSURL URLWithString:[FILESERVER2 stringByAppendingFormat:@"/%@?fileName=%@",@"data", @"yen.jpg"] ];NSURLRequest *request = [NSURLRequest requestWithURL:url];AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];#define FILESERVER2 @"http://localhost:8800"// http://<server.ip>/data?fileName=yen.jpg
  16. 16. Download File - store file pathNSString *path = @"/Users/chronoer/Desktop/tmp/yen.jpg";operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, idresponseObject) {NSLog(@"Successfully downloaded file to %@", path);} failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"Error: %@", error);}];
  17. 17. Download File - bind with progress view[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long longtotalBytesRead, long long totalBytesExpectedToRead) {long long percentDone = (totalBytesRead / totalBytesExpectedToRead);downProgressView.progress = percentDone;NSLog(@"Sent %lld of %lld bytes, %@", totalBytesWritten,totalBytesExpectedToRead, path);}];[operation start];
  18. 18. Demo• FileDownloadUpload
  19. 19. Question ?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×