[英]iPhone and asp.NET MVC File Upload with URL Parameter
this is going to be a question about sending data from the iPhone to a MVC 3 function. 这将是关于从iPhone向MVC 3功能发送数据的问题。
To get into the topic what I'm already doing and what is working, just to see the style how I'm implementing the connections. 要进入该主题,我已经在做的和在做什么的,只是看一下我如何实现连接的样式。
Here I have a sample MVC Controller function, running on my IIS 7.0. 在这里,我有一个示例MVC Controller函数,该函数在IIS 7.0上运行。
The SampleController : Controller SampleController :控制器
public ActionResult MyFunction(MyObject object) {
ContentResult result = new ContentResult();
result.Content = some content from the server encoded as JSON
return result;
}
On the iPhone I call this function like this. 在iPhone上,我这样称呼此功能。
NSString *urlString = [NSString stringWithFormat:@"https://%@:%d/%@/%@", _host, _port, Sample, MyFunction];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:json]; // some json encoded object that fits the MyObject from asp.NET
NSURLResponse *response;
NSError *error;
// synchronous so the sample code is shorter
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
// Do some error handling and http status code stuff and finally the json stuff from the NSData object
So this stuff is all working fine, but now I'm at a point where it get's confusing. 因此,这些东西都可以正常工作,但是现在我有点困惑了。 I need to upload a NSData Object, containing image Data or other stuff.
我需要上传一个NSData对象,其中包含图像数据或其他内容。 Uploading just the file using mulipart/form-data as shown in this SO answer .
如本SO答案所示,使用mulipart / form-data仅上传文件。 But when adding some url parameters it does not work anymore.
但是,当添加一些url参数时,它将不再起作用。
________________________________________________________________________________________ ________________________________________________________________________________________
So now there are two options from here to solve my problem: 因此,现在有两种方法可以解决我的问题:
The first approach is: How would a NSURLRequest look like for using a Function like this? 第一种方法是: 使用这样的功能时,NSURLRequest的外观如何? Or is this even possible?
还是有可能吗?
public ActionResult MyFunction(MyObject object, byte[] binaryData) {
// Some server stuff ect.
}
Or how would I build my NSURLRequest using my first Function and adding a file attachment? 或者我该如何使用第一个功能并添加文件附件来构建NSURLRequest?
Edit 编辑
Here is how I build my current file upload request. 这是我构建当前文件上传请求的方式。 It is basically the same I do in the second code snippet.
基本上与第二个代码段相同。
NSString *urlString = [NSString stringWithFormat:@"https://%@:%d/%@/%@", _host, _port, Sample, MyFunction];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:json];
And finally the file attachment. 最后是文件附件。 Note When only using the file attachment without parameters everything works fine, but well yeah the params are missing.
注意当仅使用不带参数的文件附件时,一切正常,但是很好,这些参数丢失了。
+ (void)attachFile:(NSData *)fileData withFilename:(NSString *)filename toRequest:(NSMutableURLRequest *)request {
NSString *boundary = @"---------------------------94712889831966399282116247425";
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--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filename]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:fileData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *oldBody = [request HTTPBody];
if (oldBody) {
[body appendData:oldBody];
}
[request setHTTPBody:body];
}
So my guess is now, that there is some boundary stuff for the parameters missing. 所以我现在的猜测是,缺少一些边界参数。
Note 注意
As an addition I use a synchronous NSURLConnection on a background Thread. 另外,我在后台线程上使用同步NSURLConnection。
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
From the given sample (please add more actual code), it is difficult to tell where there is an issue - but there are a few potential culprits. 从给定的样本(请添加更多实际代码)中,很难分辨出问题出在哪里-但是有一些潜在的罪魁祸首。
First off, the request depends on what your server will accept. 首先,请求取决于您的服务器将接受什么。
A POST request usually embeds "parameters" in the multipart/form-data body. POST请求通常在multipart / form-data主体中嵌入“参数”。 Browsers would not include "parameters" in the URL - and a server may not expect or accept this as well.
浏览器不会在URL中包含“参数”-服务器也可能不会期望或接受此参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.