我可以使用KendoUpload和WFC服务成功上传文件。问题是,当我尝试使用HttpMultipartParser解析流时,以下语句返回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"
我想知道:
以下是代码:
<form id="formAttachments" method="post">
<input class="upload" name="files" type="file" data-role="upload" data-bind="kendoUpload" />
</form>
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
};
};
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?)
}
}
}
}
答案 0 :(得分:0)
fileContents为空的原因是因为HttpMultipartParse引用了内容而不是文件。
要解决此问题,我将上传HTML元素的name属性更改为&#39; content&#39;如下图所示:
<form id="formAttachments" method="post">
<input class="upload" name="content" type="file" data-role="upload" data-bind="kendoUpload" />
</form>