Friday 15 January 2010

objective c - ASIFormDataRequest uploads blank image -


After searching here to upload a simple image with ASIFormDataRequest, it finally got work (sort), but with an unexpected error

My image has been uploaded to my server but it is empty , 0 bytes in size. Here is my code:

  - (IBAction) Upload Image: (ID) Sender {NSData * Data = UIImageJPEG Reputation (self.imageView.image, 0.6f); NSString * file = [NSTemporaryDirectory () stringByAppendingPathComponent: @ "Upload Page"]; [Data Toto File: Atomically File: Yes]; NSString * strURL = @ "http://domain.com/upload.php"; ASIFormDataRequest * Request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: strURL]]; UIImage * image1 = [UIImage imageNamed: file]; NSData * imageData1 = UIImageJPEG Report (Image 1, 1.0); [Request SetData: imageData1 withFileName: Type File and Content: @ "image / jpeg" forKey: @ "Avatar"]; [Request Security Mode: @ "Post"]; // [request appendPostData: body]; [Delegate request set: Self]; [Request setTimeOutSeconds: 13.0]; Request.shouldAttemptPersistentConnection = No; [Request SetfinSchell: @Selector (upload-request complete :)]; [Requested Setded File Selector: @Selector (Upload-Requestfield :)]; [Request Asamenus]; } - (zero) Upload request required: (ASIHTPPRXQUEST *) REQUEST {NSLOG (@ "Error - Statistics file upload is OK: \"% @ \ "", [request response string]); } - (Zero) Upload request required: (ASIHTPPRQUEQUEST *) REQUEST {NSLOG (@ "Error - DATA file upload failed: \"% @ \ "", [[request error] localization]); }   

My PHP code:

  & lt ;? Php $ target = "./"; $ Target = $ target basename ($ _FILES ['Avatar'] ['name']); $ Fine = 1; If (move_uploaded_file ($ _ files ['avatar'] ['tmp_name'], $ target)) {resonant "file is ok"; } Else {echo "Sorry, there was a problem uploading your file."; }? & Gt; First of all, I thought this was a folder permission thing, but I tested it with a simple HTML form (with the same PHP file, unpublished) and the file was full size Was uploaded in. / P> 

I know that such a method of uploading files is dangerous, but at the moment it is the only thing I have found.

Any ideas why my files are not being uploaded? Thank you.

OK, I've followed your advice and actually used FanLorkwork.

  NSDTata * ImageUpload = UIMJPEG Presentation (_imageView.image, 0.5f); FTP client * client = [FTP client client with URL: [NSL URL URL Authorization: @ "http://domain.com"]]; NSMutableURLRequest * Request = [Customer MultipartFormRequestWithMethod: @ "Post" Path: @ "upload.php" Parameter: Zero Construction: BodyWithBlock: ^ (ID & Lt; AFMultipartFormData> FormData) {[formData appendPartWithFileData: imageToUpload Name: @ "Avatar "Filename: fullFilename mimeType: @" image / jpeg "]; }]; AFHTTPRequestOperation * Operation = [[AFHTTPRequestOperation alloc] initWithRequest: Request]; [Operation Settlement BlockWithSoftware: ^ (FHTPPKAEASTA Operation * Operation, ID response object) {nssting * response = [operation response string]; NSLog (@ "response: [% @]", response); [MBP displayHUD hide HUDForView: animated self.view: yes]; } Failure: ^ (FHTPPKAEASTA Operation * Operation, NSERR * Error) {if ([Operation. Response Status] == 403) {NSLog (@ "Upload Failed"); Return; } NSLog (@ "Error:% @", [Operation Error]); }]; [Start operation];    

No comments:

Post a Comment