xcode 将图像和文本从 iPhone 发布到 asp.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6136288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post image and text from iPhone to asp.net
提问by OlavJ
I am trying to send images and text from an iPhone application to a asp.net webpage.
我正在尝试将图像和文本从 iPhone 应用程序发送到 asp.net 网页。
By following this exampleI now have the following method in xcode for uploading an image:
按照这个例子,我现在在 xcode 中有以下方法来上传图像:
- (BOOL) uploadData {
NSData *imageData = UIImageJPEGRepresentation([serviceForm image], 0.9);
NSString *urlString = @"http://someUrl/SubmitSchema.ashx";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
BOOL success = [returnString isEqualToString:@"ok"];
[returnString release];
return success;
}
At the bottom on the same article there is a suggestion about creating an asp.net generic handler. Based on this, I have the following simple code for saving the image:
在同一篇文章的底部,有一个关于创建一个 asp.net 通用处理程序的建议。基于此,我有以下用于保存图像的简单代码:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
foreach (var name in context.Request.Files.AllKeys)
{
var file = context.Request.Files.Get(name);
SaveFile(file);
}
}
private void SaveFile(HttpPostedFile file)
{
var fileName = Path.GetFileName(file.FileName);
var path = "C:\Test\" + fileName;
file.SaveAs(path);
}
This works and the image is saved, but I have not yet managed to include a string of text in the same post. The text I want to send can be quite long, and I suppose it is better to include this in the posted data rather than as a query string?
这有效并且图像被保存,但我还没有设法在同一个帖子中包含一串文本。我想发送的文本可能很长,我认为最好将其包含在发布的数据中而不是作为查询字符串?
How would you add some text to the posted data and read it in .net?
您将如何向发布的数据添加一些文本并在 .net 中读取它?
回答by mdm
I'm not an Objective-C expert but from a little Googling I found a discussion and a working example of sending a file and form fields in a single request.
我不是 Objective-C 专家,但通过一点谷歌搜索,我发现了一个讨论和一个在单个请求中发送文件和表单字段的工作示例。
On the server, you can pull the data out again on the other side using:
在服务器上,您可以使用以下方法在另一端再次拉出数据:
string data = Request.Form["name"];
If you get stuck, Fiddleris a great tool for sniffing HTTP requests.
如果您遇到困难,Fiddler是一个很好的嗅探 HTTP 请求的工具。
回答by Ankur
One option is to send the long string with content type as form encoded content then using context.Request.Form to get the value of the posted string. As you have a large string it can be put in a single form value as "data='large string'" and in server side you can do context.Request.Form["data"] to get the string. NOTE: you have to properly encode the string so that any = or & are encoded before being posted
一种选择是将具有内容类型的长字符串作为表单编码内容发送,然后使用 context.Request.Form 获取发布字符串的值。由于您有一个大字符串,它可以作为“data='large string'”放在单个表单值中,并且在服务器端您可以执行 context.Request.Form["data"] 来获取字符串。注意:您必须正确编码字符串,以便在发布之前对任何 = 或 & 进行编码