Kendo UI 上传:如何使用 WCF 服务从多部分表单上传中提取文件内容

Kendo UI Upload : how to extract file content from a multi-part form upload using WCF service

我可以使用 KendoUpload 和 WFC 服务成功上传文件。问题是,当我尝试使用 HttpMultipartParser 解析流时,以下语句 return null:

byte[] content = parser.FileContents;

文件的实际内容是通过这条语句得到的:

HttpUtility.UrlDecode(parser.Parameters["files"]) which returns:

"ilename=\"Test1.txt\"\r\nContent-Type: text/plain\r\n\r\nSome text END"

我想知道:

  1. 为什么 parser.FileContents 设置为空?
  2. 为什么 parser.Parameters["files"] 嵌入元数据?我希望看到我的测试文件的内容只是 'Some text END'。

代码如下:

HTML:

<form id="formAttachments" method="post">
    <input class="upload" name="files" type="file" data-role="upload" data-bind="kendoUpload" />
</form>

JavaScript:

            var upload = $(".upload").kendoUpload({

              async: {
                saveUrl: 'UploadFormAttachment',
                autoUpload: false
              },
              multiple: true,
              showFileList: true,
              upload: onFormAttachmentUpload,
            });

        self.onFormAttachmentUpload = function (e) {
           var attachment = $.grep(self.formAttachments(), function (a) {
            return a.uid == e.files[0].uid;
            })

            var importId = self.importId;
            var formId = fm.currentFormDef.id;
            var fileName = e.files[0].name;
            var title = attachment[0].title;
            var description = attachment[0].description;

          e.data = {
              importId: importId,
              formId: formId,
              fileName: fileName,
              title: title,
              description: description
          };
    };

WCF 服务

    public UploadResponse UploadFormAttachment(Stream uploadRequest)
{
    try
    {
        using (MemoryStream stream = new MemoryStream())
        {
            uploadRequest.CopyTo(stream);
            stream.Position = 0;

            HttpMultipartParser parser = new HttpMultipartParser(stream, "content");
            if (parser.Success)
            {
                string importId = HttpUtility.UrlDecode(parser.Parameters["importid"]);
                int formId = Convert.ToInt32(HttpUtility.UrlDecode(parser.Parameters["formid"]));
                string fileName = HttpUtility.UrlDecode(parser.Parameters["filename"]);
                string title = HttpUtility.UrlDecode(parser.Parameters["title"]);
                string description = HttpUtility.UrlDecode(parser.Parameters["description"]);
                byte[] content = parser.FileContents;

                // parser.FileContents is null!!!
                // HttpUtility.UrlDecode(parser.Parameters["files"]) return content and embedded meta-data (how to get raw data only?)

            }
        }
    }           
}

fileContents 为空的原因是因为 HttpMultipartParse 引用了 content 而不是 files.

为了解决这个问题,我将上传 HTML 元素的名称属性更改为 'content',如下所示:

<form id="formAttachments" method="post">
    <input class="upload" name="content" type="file" data-role="upload" data-bind="kendoUpload" />
</form>