用于上传图像文件从iOS系统ASP.NET什么请求头?图像文件、上传、系统、iOS

2023-09-06 17:52:40 作者:誓言只是一时的失言

我建立一个iOS应用程序,我想上传图片到像一个网页文件的发布的ASP.NET MVC的服务器组件。

我已经创建iOS中的NSMutableURLRequest和NSConnection连接对象,并验证了呼叫的.NET服务器组件的工作。

  NSMutableURLRequest *请求= [[NSMutableURLRequest页头]初始化];
[请求setTimeoutInterval:(间隔== 0 SERVICE_DEFAULT_TIMEOUT_INTERVAL:区间);
[请求setURL:[NSURL URLWithString:[的NSString stringWithFormat:@%@%@,SERVICE_BASEURL,URL]]];
[请求setHTTPMethod:@POST];
...
 

图片数据是在一个NSData对象从AVFoundation模块

 的NSData *为imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRe presentation:imageSampleBuffer]。
 

在服务器中的.NET组件控制器方法

  [HttpPost]
公共JsonResult UploadMedia(HttpPostedFileBase的FileData)
{
 ...
  返回JSON(@成功);
}
 
同步推玩机 Android与iOS手机照片互传技巧分享

的目标是图象数据传输到服务器并存储它。我的问题是:

还有什么其他HttpHeaderField值需要创造? 我应该如何嵌入在NSMutableURLRequest的图像数据? 应该是什么类型的参数是在.NET组件的方法? 基于在#3的类型,我应该如何提取服务器组件的图像数据? 解决方案

您可以发布数据使用多部分表单数据。

  NSMutableURLRequest *请求= [NSMutableURLRequest requestWithURL:网址
                                        的CachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:600]

request.HTTPMethod = @POST;

的NSString *边界= @--------------- AF7DAFCDEFAB809;
NSMutableData *数据= [NSMutableData dataWithCapacity:300 * 1024];

[数据appendData:[[NSString的stringWithFormat:@内容处置:表格数据;名称= \%@ \文件名= \image.jpg的\\ r \ñ\ r \ N,@场名称]
                             dataUsingEncoding:NSUTF8StringEncoding]];
[数据appendData:imgdata]。 //数据上传
[数据appendData:[[NSString的stringWithString:@\ r \ N]
                          dataUsingEncoding:NSUTF8StringEncoding]];

[数据appendData:[[NSString的stringWithFormat:@ - %@ \ r  -  \ N,边界]
                  dataUsingEncoding:NSUTF8StringEncoding]];

request.HTTPBody =数据;
 

I am building an iOS app where I want to upload images to an ASP.NET MVC server component like the posting of a file in a web page.

I have created the NSMutableURLRequest and NSConnection objects in iOS and have verified the call the the .NET server component is working.

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:(interval==0?SERVICE_DEFAULT_TIMEOUT_INTERVAL:interval)];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVICE_BASEURL,url]]];
[request setHTTPMethod:@"POST"];
...

The image data is in an NSData object from an AVFoundation module

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];

On the server the controller method in the .NET component is

[HttpPost]
public JsonResult UploadMedia(HttpPostedFileBase fileData)
{
 ...
  return Json(@"Success");
}

The goal is to transfer the image data to the server and store it. My questions are:

What other HttpHeaderField values need to be created? How should I embed the image data in the NSMutableURLRequest? What type should be parameter be in the .NET component method? Based upon the type in #3 how should I extract the image data in the server component?

解决方案

you can post data use multi-part form-data.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                                        cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                   timeoutInterval:600];

request.HTTPMethod = @"POST";

NSString *boundry = @"---------------AF7DAFCDEFAB809";
NSMutableData *data = [NSMutableData dataWithCapacity:300 * 1024];

[data appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n\r\n", @"field name"]
                             dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:imgdata];   // data upload
[data appendData:[[NSString stringWithString:@"\r\n"] 
                          dataUsingEncoding:NSUTF8StringEncoding]];

[data appendData:[[NSString stringWithFormat:@"--%@\r--\n",boundry] 
                  dataUsingEncoding:NSUTF8StringEncoding]];

request.HTTPBody = data;