我曾尝试在 asp.net 中使用 jquery ajax 文件上传脚本将文件上传到文件夹。我显示自定义文件上传图像而不是文件上传控件。这种方法在 Firefox 和 chrome 中运行良好。但在 IE 中它不起作用。我的代码如下:
<img src="Images/attach.png" title="Add Attachment" style="vertical-align: middle; height:24px;cursor:pointer; " onclick="document.getElementById('fileToUpload').click();" id="attach" alt=""/> <img src="images/ajax-loader.gif" style="height:24px;vertical-align: middle;display:none;" id="noteloading" alt=""/> <asp:FileUpload ID="fileToUpload" runat="server" style="display:none;" ClientIDMode="Static" onchange="getFileName()" />
<input type="button" value="Submit" onclick="ajaxFileUpload()" />
脚本:
function ajaxFileUpload()
{
var fileName = $('#<%=fileToUpload.ClientID %>').val().replace(/.*(\/|\\)/, '');
var user = '<%= User.Identity.Name%>';
$('#NotesStatus').val('');
$(obj).hide();
if (fileName != "" && fileName != null) {
$.ajaxFileUpload({ url: 'AjaxFileUploader.ashx',
secureuri: false,
fileElementId: 'fileToUpload',
dataType: 'json',
success: function (data, status) {
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
SaveNotesData(entryId, formId, user, action, comments, actDate, relatedData, data.msg);
}
}
},
error: function (data, status, e) {
alert(e);
alert('hai');
}
});
}
else {
SaveNotesData(entryId, formId, user, action, comments, actDate, relatedData, fileName);
}
}
function SaveNotesData(entryId, formId, user, action, comments, actDate, relatedData, fileName)
{
$.ajax({ url: "AjaxService.asmx/AddEntryNotes", contentType: "application/json; charset=utf-8", type: "POST",
data: '{"entryid":' + entryId + ', "frmid":"' + formId + '","user":"<%= User.Identity.Name%>","action":"' + action + '","comments":"' + comments + '","actDate":"' + actDate + '", "relatedData":"'+relatedData+'", "fileName":"' + fileName + '"}',
success: function (result) { $('#imgprogressing').hide();
if (result == null || result.d == null) return;
if (result.d == "true")
{
$('#NotesStatus').append("Notes Added Successfully");
$('.usernotes').load("NotesPopup.aspx?formId=" + encodeURI(formId) + "&entryId=" + encodeURI(entryId) + "&uid="+(new Date()).getTime());
$('.toolTip').each(function (index) {
attachNotes($(this).attr("entryid"), $(this).attr("frm"), $(this));
});
$('#<%=divAddNote.ClientID %>').dialog('close');
}
else
{
alert("Failure in Notes Addition !");
}
//getNotes(entryId, formId, $('.usernotes'));
//HideNotesPopup();
document.getElementById('hlNoteSubmit').style.visibility = 'visible';
$('#NotesStatus').html(''); $('#<%=taComments.ClientID %>').val('');
$('#<%= actionDate.ClientID %>').val($.datepicker.formatDate('m/d/yy', new Date()));
},
error: function (XMLHttpRequest, textStatus, errorThrown) { $('#imgprogressing').hide(); alert('Loading failed!!'); }
});
}
AjaxFileUploader.ashx:
if (context.Request.Files.Count > 0)
{
string path = context.Server.MapPath("~/Uploads");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var file = context.Request.Files[0];
string fileName;
if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
{
string[] files = file.FileName.Split(new char[] { '\\' });
fileName = files[files.Length - 1];
}
else
{
fileName = file.FileName;
}
string newFilename = Guid.NewGuid().ToString();
FileInfo fInfo = new FileInfo(fileName);
newFilename = string.Format("{0}{1}", newFilename, fInfo.Extension);
string strFileName = newFilename;
fileName = Path.Combine(path, newFilename);
file.SaveAs(fileName);
string msg = "{";
msg += string.Format("error:'{0}',\n", string.Empty);
msg += string.Format("msg:'{0}'\n", strFileName);
msg += "}";
context.Response.Write(msg);
}
当我显示文件上传控件并浏览文件上传控件时,它在 ie 中工作没有问题。而不是当我设置 filupload 控件时显示 none 并通过自定义图像点击触发 fileupload 点击不是所有工作在 ie 中。但是在 Firefox 和 chrome 中运行良好。如何解决这个问题?