简体   繁体   中英

sending a pdf file from iphone to web server using AFNetworking?

The following code are to send an image file to web server but I need to change it to send a pdf file. Where to change I have no idea. Any help..

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];

Please check this link . I think this what you would need . But first you have to store the PDF file in the documents directory.

Upload pdf file (from document directory) through web service in iPhone?

This code uses AFNetworking

Not sure if it's error free, because it has been taken from a longer source, so I've modified some pieces of code, but it's a good start

Hope this helps.

NSData is for storing data generally, not just images. If you create a NSData object containing PDF data instead then you will able to use the same code to upload it. You don't have to have the PDF file in a specific place as long as you can access it - though dataWithContentsOfFile will do fine if you do have a file. Many apps create PDF output from ther own displays using a PDF graphics context for instance, with UIGraphicsBeginPDFContextToData .

Here's some code showing how to send the PDF using AFNetworking (see this for correct mime type etc.) - that snippet is being used to illustrate a bug in AFNetworking, but don't worry about that, it's fixed.

You can convert your pdf file into NSData and send it to web service

You simply need to do this:

NSData *pdfData = [NSData dataWithContentsOfFile:pdfName];

You can have a web service that will take this data and convert it back to PDF at server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM