使用ASIFormDataRequest将图像上传到服务器 [英] Uploading image to server with ASIFormDataRequest
问题描述
您好为我的iPhone应用程序构建服务,并且需要具有从用户获取图像的功能:直到现在,我一直只使用这种方法获取文本:
Hi build a service for my iPhone app and need to have function that get image from the user: Until now i used to get only text with this method:
string input = null;
using (StreamReader sr = new StreamReader(Request.InputStream))
{
input = sr.ReadToEnd();
}
我应该如何从帖子中获取图片?
How should i get the image from the post?
这是我从iphone应用发送图像的方式:
This is how i send the image from the iphone app:
imageRequest = [ASIFormDataRequest requestWithURL:url];
[imageRequest addData:imageData withFileName:@"someFileName.jpeg" andContentType:@"image/jpeg" forKey:@"uploadedImage"];
[imageRequest setDelegate:self];
[imageRequest setRequestMethod:@"POST"];
[imageRequest startAsynchronous];
推荐答案
您无法通过添加具有名称的图像来上传图像.首先将UIImage对象转换为Base64Encoding字符串,然后将该字符串发送到服务器.
You cannot upload image by adding image with name. First convert UIImage object into Base64Encoding string and then send that string to server.
从链接在此处输入链接描述
以及使用这些方法
- (NSString*) stringFromImage:(UIImage*)image
{
if(image){
NSData *dataObj = UIImagePNGRepresentation(image);
return [dataObj base64Encoding];
} else {
return @"";
}
}
- (UIImage*) imageFromString:(NSString*)imageString
{
NSData* imageData =[NSData dataWithBase64EncodedString:imageString];
return [UIImage imageWithData: imageData];
}
还要在Base64中检查此链接以获取图像到字符串和字符串至图像
Also check this link for Image to string and string to image in Base64 image conversion
对您有帮助
这篇关于使用ASIFormDataRequest将图像上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!