我想将图像从 iphone/ipad 发布到 .net 服务。目前我正在使用 ASIFormDataRequest 但我收到错误
“请求格式无效:multipart/form-data;charset=utf-8;boundary=0xKhTmLbOuNdArY。”
请帮我解决这个错误。谢谢
-(void)uploadFile{
NSURL *url = [NSURL URLWithString: photoUploadURLString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
//if you have your site secured by .htaccess
NSString *fileName = [NSString stringWithFormat:@"ipodfile%@.jpg",self.fileID];//your image file name
[request addPostValue:fileName forKey:@"name"];
// Upload an image
NSData *imageData = UIImageJPEGRepresentation([UIImage imageName:fileName]) // your image
[request setData:imageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"userfile"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[request startAsynchronous];
}
- (void)uploadRequestFinished:(ASIHTTPRequest *)request{
NSString *responseString = [request responseString];
NSLog("Upload response %@", responseString);
}
- (void)uploadRequestFailed:(ASIHTTPRequest *)request{
NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]);
}
Try to set data in your ASIFormDataRequest
like this
NSData *imgData = UIImagePNGRepresentation(img);
[request addData:imgData withFileName:@"FILENAME" andContentType:@"image/jpg" forKey:@"YOUR KEY"];
Where request is your ASIFormDataRequest
Happy coding.
我也遇到了同样的问题。。同样的错误。。
但我用一些不同的方法解决了它。
基本上 .Net webservice 期望图像作为binary
或byte[]
数组提供。
我找不到将UIImage
as byte[]
using发送ASIFormDataRequest
到服务器的方法。
但是NSURLRequest
以更好的方式,我们需要比平时多挖一点。
我在使用Sudzc
iOS 的肥皂框架方面有过很好的经验,这不是开源的,但你可以免费获得它。
你需要做的就是,
这将为您提供一个包含 iOS 项目的 zip 文件。一旦你打开项目,检查Generated
你可以看到所有你需要的目录。