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

时间:2022-01-12 23:01:34

I can successfully upload a file using the KendoUpload and WFC service. The issue is that when I attempt to parse the stream using HttpMultipartParser the following statement return null:

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

byte[] content = parser.FileContents;

The actual content of the file is obtained through this statement:

该文件的实际内容是通过以下声明获得的:

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

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

I would like to know:

我想知道:

  1. Whythe parser.FileContents is set to null?
  2. 为什么parser.FileContents设置为null?

  3. Why is parser.Parameters["files"] embedding meta-data? I was expecting to see the content of my test file as simply 'Some text END'.
  4. 为什么parser.Parameters [“files”]嵌入了元数据?我期待看到我的测试文件的内容只是'Some text END'。

Here's the code:

这是代码:

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 service

    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?)

            }
        }
    }           
}

1 个解决方案

#1


0  

The reason why fileContents was null is because the HttpMultipartParse was making a reference to content instead of files.

fileContents为null的原因是因为HttpMultipartParse正在引用内容而不是文件。

To fix the problem I changed the name attribute of the Upload HTML element to 'content' as shown below:

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

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

#1


0  

The reason why fileContents was null is because the HttpMultipartParse was making a reference to content instead of files.

fileContents为null的原因是因为HttpMultipartParse正在引用内容而不是文件。

To fix the problem I changed the name attribute of the Upload HTML element to 'content' as shown below:

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

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