将System从iphone发布到.NET webservice时出现'System.InvalidOperationException:请求格式无效:multipart / form-data'错误

时间:2010-01-14 21:34:41

标签: .net objective-c web-services file-upload

我正在尝试将图片从iphone应用发布到.Net webservice,我遇到了这个错误。我已经根据this kb article更新了我的web.config,我可以成功发布到将字符串作为参数的方法。我的问题是尝试使用图像发布数据。我已尝试发布this waythat way,但我最终都遇到了同样的错误:


System.InvalidOperationException: Request format is invalid: multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY.
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

这是我的网络服务签名:


[WebMethod]
public XmlDocument UploadImageToServer(string usertoken, byte[] image)
{ 
   //stuff happens in here
}

..这是我最近的尝试:


- (void)sendImageToServer:(NSURL *)serivceURL withUserToken:(NSString *)usertoken
{
 NSData *imageData = UIImageJPEGRepresentation(self.selectedImage, 1.0f);

 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:serviceURL] autorelease];
 [request setPostValue:usertoken forKey:@"usertoken"];
 [request setData:imageData forKey:@"image"];
 [request setDelegate:self];
 [request startAsynchronous];

 NSLog(@"We set the request out!");

}

此外,我将httpRuntime最大请求长度增加到40MB(<httpRuntime maxRequestLength="40960"/>)只是为了确保问题不是我的图像大小,但错误仍然存​​在。

任何帮助都将不胜感激。

-a

3 个答案:

答案 0 :(得分:6)

我通过将它们移动到web.config中的行来实现这一点:

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

它们位于web.config中<system.webServer><handlers>部分的底部,但是因为我将它们移到顶部它似乎工作!

答案 1 :(得分:2)

您正在尝试将二进制数据作为输入参数读取。您需要从Context.Request中读取参数。换句话说,删除userToken和image作为请求参数。应该通过Context.Request [“userToken”]访问userToken,并且应该通过Context.Request.PostedFiles [“image”]提供图像: http://www.rahulsingla.com/blog/2010/07/ext-net-ajax-file-upload-using-web-service

答案 2 :(得分:0)

谢谢r_honey,

一些额外的代码

[WebMethod]
    public string UploadFile()
    {
    //if you take parameter in UploadFile() like UploadFile(string cropName, .....), then give error System.InvalidOperationException: Request format is invalid: multipart/form-data
        string ret = "";
        HttpRequest request = this.Context.Request;
        HttpPostedFile file = request.Files["upload"];
        string FileName = file.FileName;
        string cropName = request["cropName"];

        string ext = Path.GetExtension(FileName).ToLower();

        if (!(ext == ".png" || ext == ".jpg" || ext == ".jpeg"))// for only images file
        {
           ret = string.Format("File extension {0} not allowed.", ext);

            return ret;
        }

        if (FileName != "")
        {
            string path = HttpRuntime.BinDirectory;

            string UUID = System.Guid.NewGuid().ToString();
            string filepath = path + "upload/" + UUID + ".jpg";
            file.SaveAs(filepath);
            // add your code if any
        }
    }