【问题标题】:WCF service contract to receive file upload from ExtJs front endWCF 服务合同从 ExtJs 前端接收文件上传
【发布时间】:2012-04-18 12:45:53
【问题描述】:

我查看了现有问题,但找不到完全匹配的问题。也许我遗漏了一些明显的东西,如果是这样 - 请在正确的地方指导我。这是我的问题:

我有 ExtJs 前端和需要将二进制数据上传到 WCF 后端的应用程序。在前端我有以下代码(我使用 Ext.form.field.File 控件让用户选择一个文件)

// Create a dummy form in the controller after user selected a file
var form = Ext.create('Ext.form.Panel', {
  items: [ my_file_field ]
});

form.getForm().submit({
  method: 'POST',
  url: 'myservice.url',
  ...
});

在后端我有以下合同:

namespace MyApp
{
    [ServiceContract]
    public interface ITransferService
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "UploadImage", Method = "POST")]
        void SaveImage(Stream buffer);
    }

}

除了一件小事之外,它工作得“很好”:在SaveImage() 内部的流中,我不仅从用户选择的文件中获得二进制数据,而且还获得一堆标题和编码字段:

------WebKitFormBoundary26wAkvwGTnAMELFM
Content-Disposition: form-data; name="ext-gen1654"; filename="photo.png"
Content-Type: application/octet-stream
..... binary data goes here .... 
------WebKitFormBoundary26wAkvwGTnAMELFM--

我错过了什么?如何更改服务合同以获得干净的二进制数据?

【问题讨论】:

    标签: wcf extjs file-upload upload extjs4


    【解决方案1】:

    我发现了这个帖子:http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

    有人知道更简单的解决方案吗?并不是说提出的解析逻辑很复杂,但我的印象是这是一项常见的任务,应该有更简单的方法来完成它。我不敢相信 MSFT 不理解 WCF 中开箱即用的标准网页 POST...

    【讨论】:

      【解决方案2】:

      这应该将附加到当前请求的文件返回给您,但文件键或索引。希望这会有所帮助!

      Stream file = HttpContext.Current.Request.Files.Get(<file_key>).InputStream;
      

      【讨论】:

      • @sha 这样做:HttpContext.Current.Request.Form.Get("myParam")。或者,您也可以从查询字符串中读取它们:HttpContext.Current.Request.QueryString.Get("myParam").
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多