我可以使用 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"
我想知道:
- 为什么 parser.FileContents 设置为 null?
- 为什么 parser.Parameters["files"] 嵌入元数据?我期待看到我的测试文件的内容只是“一些文本结束”。
这是代码:
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?)
}
}
}
}