gpt4 book ai didi

jquery - 带分块的编程文件上传 : Only the first file is sent

转载 作者:行者123 更新时间:2023-12-03 22:10:48 27 4
gpt4 key购买 nike

当做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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com