我试图通过afnetworking将相机拍摄的照片上传到网络服务。
这是我的上传部分代码:
CGSize newSize = CGSizeMake(500.0f, 500.0f);
UIGraphicsBeginImageContext(newSize);
[_imagedata drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imgData = UIImageJPEGRepresentation(newImage, 0.9f);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"user": "test_user"};
[manager POST:@"http://www.mywebsite.com/webservice.aspx" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[formData appendPartWithFormData:imgData name:@"image"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我使用Asp.net作为我的网络服务。
这是我的服务器端代码:
string USER = Server.UrlDecode(Request["user"]),
SqlParameter prmUser = sc.Parameters.Add("@user", SqlDbType.VarChar);
prmUser.Direction = ParameterDirection.Input;
prmUser.Value = USER;
HttpFileCollection MyFileCollection = Request.Files;
if (MyFileCollection != null && MyFileCollection.Count > 0 && MyFileCollection[0] != null)
{
SqlParameter prmImage = sc.Parameters.Add("@Image", SqlDbType.Image);
prmImage.Direction = ParameterDirection.Input;
byte[] buf = new byte[MyFileCollection[0].ContentLength];
MyFileCollection[0].InputStream.Read(buf, 0, MyFileCollection[0].ContentLength);
prmPhoto.Value = buf;
}
sc.ExecuteNonQuery();
现在我每次运行程序都会出现此错误:
Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo=0x1555ff60 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x16a94010> { URL: http://www.mywebsite.com/webservice.aspx } { status code: 500, headers {
Connection = close;
"Content-Length" = 3420;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 01 Apr 2015 15:56:21 GMT";
Server = "Microsoft-IIS/8.5";
Via = "1.1 magellan-front (squid/3.5.1)";
"X-AspNet-Version" = "4.0.30319";
"X-Cache" = "MISS from magellan-front";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://www.mywebsite.com/webservice.aspx, NSLocalizedDescription=Request failed: internal server error (500),
详细错误:
[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (image="...�5�Ƨ&�����<I
�(�ep��K�,�=mp�...").]
web服务也适用于android HttpFileUpload 方法。
答案 0 :(得分:2)
我试图找到这个的基础知识,这里是link
您的代码似乎是客户端似乎有问题。为什么不尝试使用appendPartWithFileURL:name:fileName:mimeType:error:
,并说如果不起作用则问题必须来自服务器端。
我对ASP.NET知之甚少,但您能否验证一下您尝试读取文件的方式。这是link我发现了一些你可能想尝试实现的有趣内容。
HttpFileCollection
上的枚举器返回文件的键(名称),而不是HttpPostedFileBase
个对象。获得密钥后,使用带有密钥(文件名)的Item
([]
)属性来获取HttpPostedFileBase
对象。
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
...
}
如果这不起作用,请告诉我。
答案 1 :(得分:1)
尝试将图片上传为文件(appendPartWithFileData
)而不是表单数据(appendPartWithFormData
),并设置内容类型:
[formData appendPartWithFileData:imgData name:@"image"
fileName:@"image.jpg" mimeType:@"image/jpeg"];