我想上傳一個在ASP.NET中使用XMLHttpRequest和FormData的Excel文件,但總是在ASP.NET中將Request.FIles設置爲null,這裏是我的代碼,請幫助我,Request.Files即將到來ASP.NET
function BulkUploadUsers(e){
var url = "/BC/Product/Modules/UserManagement/BulkUpload.aspx?action=import";
var fd = new FormData();
fd.append("ImportWorkOrderExcelFile", document.getElementById('ctl01_ImportFcpFile').files[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('X-CSRF-Token', document.getElementById("_RequestVerificationToken").value);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(fd);
}
<div>
<input type="file" id="ImportFcpFile" runat="server" onchange="BulkUploadUsers(this)"/>
</div>
在Asp.net頁面
protected void Page_Load(object sender, EventArgs e)
{
try
{
var aa = FindControl("ImportFcpFile");
HttpPostedFile file = Request.Files["ImportUserExcelFile"];
}
catch (Exception ex)
{
}
}
沒有什麼所謂的'ImportUserExcelFile'在AJAX代碼保存。 – David
使用Request.Files [0]來訪問文件 –
我寧願使用HttpFileCollection fileCollection = HttpContext.Current.Request.Files; –