- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当做programmatic file upload时使用jQuery-File-Upload plugin启用分块后,我无法发送多个文件。
我调用电话的方式如下:
fileUploadWidget.fileupload('send',
{
files: filesList
})
filesList
是 File 对象的列表。
此外,我还设置了 maxChunkSize 并将 singleFileUploads 设置为 true(我也尝试过 false),如 Options wiki page 所示。 .
有人成功地让它发挥作用吗?
更新:
我做了一个issue在 GitHub 上解决了这个问题,以下是作者的回复:
<小时/>[...] Chunked uploads only support one file per request. That is, you can still simultaneously upload multiple files in chunks, but you'll have to send mutliple requests for that.
我们的解决方案:
正如已经评论过的,我们最终要做的就是在循环中发送文件,其中 widget
使用 sequentialUploads 进行初始化。设置为 true
(您需要根据后端的配置方式进行设置):
// for illustration only
// does not do anything with the returned jqXHR objects
for (i = 0; i < files.length; i++) {
widget.fileupload('send', { files: files[i] });
}
最佳答案
如果您使用 C# 作为后端,您可以尝试此(客户端、JQuery):
$("#btnUpload").click(function () { // btnUpload is the ID of any button for upload
var formdata = new FormData(); //FormData object
var fileInput = document.getElementById('fileInput');
//Iterating through each files selected in fileInput
for (i = 0; i < fileInput.files.length; i++) {
//Appending each file to FormData object
formdata.append(fileInput.files[i].name, fileInput.files[i]);
}
//Creating an XMLHttpRequest and sending
var xhr = new XMLHttpRequest();
xhr.open('POST', '/UploadMethod'); // UploadMethod is your back-end code
xhr.send(formdata);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
// alert("uploaded");
}
}
});
然后在 UploadMethod 中执行如下操作:(ServerSide,C#)
public void Upload()
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i]; //Uploaded file
//Use the following properties to get file's name, size and MIMEType
int fileSize = file.ContentLength;
string fileName = file.FileName;
string mimeType = file.ContentType;
System.IO.Stream fileContent = file.InputStream;
//To save file, use SaveAs method
var appPath = HostingEnvironment.ApplicationPhysicalPath + "Documents/" + "_" + DateTime.Now+ fileName;
file.SaveAs(appPath); //File will be saved in Documents folder
}
}
关于jquery - 带分块的编程文件上传 : Only the first file is sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18368823/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!