在下面的代码我有一个动态文件上传我必须使用ajax上传多个文件。我尝试了下面的代码,但请求文件计数为0.请帮我解决这个问题。
<input id="Button1" type="button" class="btn btn-primary" value="Add Files" onclick = "AddFileUpload()" />
function AddFileUpload() {
var div = document.createElement('DIV');
div.innerHTML = '<input id="file' + counter + '" multiple name = "file' + counter +
'" type="file" />' +
'<input id="Button' + counter + '" type="button" ' +
'value="Remove" onclick = "RemoveFileUpload(this)" />' + '<input id="BtnUpld' + counter + '" value="Upload" name = "BtnUpld' + counter +
'" onclick = "Upload()" type="Button" />';
document.getElementById("FileUploadContainer").appendChild(div);
counter++;
}
function Upload() {
$.ajax({
type: "POST",
url: "NewTask.aspx/Upload",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
}
});
<div id = "FileUploadContainer">
</div>
[WebMethod]
public static void Upload()
{
if (HttpContext.Current.Request.Files.Count > 0)
{
}
}
答案 0 :(得分:0)
您必须将文件附加到某个地方
您有两个选择:
AJAX
和base64
格式发送(或使用FormData
https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects - 不是跨平台)。Form
发送此类答案(http://forums.asp.net/t/1569041.aspx?add+dynamically+fileupload+control+#3928265,我无法在此发布完整代码)