我正在努力让这个工作,但我在上传文件时遇到错误。
ASPX
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="file-upload-dialog" />
<asp:Button runat="server" ID="btnUpload" CssClass="btn upload" Text="Upload" />
处理程序
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "multipart/form-data";
context.Response.Expires = -1;
try
{
HttpPostedFile postedFile = context.Request.Files["file"];
string savepath = HttpContext.Current.Server.MapPath("~/assets/common/CompetitionEntryImages/");
var extension = Path.GetExtension(postedFile.FileName);
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);
var id = Guid.NewGuid() + extension;
if (extension != null)
{
var fileLocation = string.Format("{0}/{1}",
savepath,
id);
postedFile.SaveAs(fileLocation);
context.Response.Write(fileLocation);
context.Response.StatusCode = 200;
}
}
catch (Exception ex)
{
context.Response.Write("Error: " + ex.Message);
}
}
jQuery
$(document).ready(function () {
email = $("input[id$='emailHV']").val();
alert(email);
$('#aspnetForm').attr("enctype", "multipart/form-data");
});
$('#<%= btnUpload.ClientID %>').on('click', function (e) {
e.preventDefault();
var fileInput = $('#ctl00_PageContent_Signup_ctl06_MWFileUpload_FileUpload1');
var fd = new window.FormData();
fd.append('file', fileInput.files[0]);
$.ajax({
url: '/charity-challenge/MWFileUploadHandler.ashx',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
alert(data);
}
});
});
错误
HTML
<input type="file" name="ctl00$PageContent$Signup$ctl06$MWFileUpload$FileUpload1" id="ctl00_PageContent_Signup_ctl06_MWFileUpload_FileUpload1" class="file-upload-dialog">
<input type="submit" name="ctl00$PageContent$Signup$ctl06$MWFileUpload$btnUpload"
value="Upload" onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions("ctl00$PageContent$Signup$ctl06$MWFileUpload$btnUpload",
"", true, "", "", false, false))"
id="ctl00_PageContent_Signup_ctl06_MWFileUpload_btnUpload" class="button">
编辑
最后,我通过做这些事情来形成数据让它工作了
var fileData = fileInput.prop("files")[0]; // Getting the properties of file from file field
var formData = new window.FormData(); // Creating object of FormData class
formData.append("file", fileData); // Appending parameter named file with properties of file_field to form_data
formData.append("user_email", email);
完整的工作代码
$('#<%= btnUpload.ClientID %>').on('click', function (e) {
e.preventDefault();
var fileInput = $('#<%= FileUpload1.ClientID %>');
var fileData = fileInput.prop("files")[0]; // Getting the properties of file from file field
var formData = new window.FormData(); // Creating object of FormData class
formData.append("file", fileData); // Appending parameter named file with properties of file_field to form_data
formData.append("user_email", email);
$.ajax({
url: '/charity-challenge/MWFileUploadHandler.ashx',
data: formData,
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
var obj = $.parseJSON(data);
if (obj.StatusCode == "OK") {
$('#<%= imagePath.ClientID %>').val(obj.ImageUploadPath);
$('.result-message').html(obj.Message).show();
} else if (obj.StatusCode == "ERROR") {
$('.result-message').html(obj.Message).show();
}
},
error: function (errorData) {
$('.result-message').html("there was a problem uploading the file.").show();
}
});
});