我按照以下网址中的示例说明了如何使用jQuery Form插件将异步文件上传到我的.NET MVC控制器。 (http://aspzone.com/tech/jquery-file-upload-in-asp-net-mvc-without-using-flash/)第一次上传文件后第一次我通过返回的PartialView替换表单所在的div时,一切都运行得很好。当替换div时,我调用一个javascript函数来重建ajaxForm对象,但这似乎是事情停止工作的地方。当代码返回第一次我得到我的替换div就好了,外观是正确的,但javascript代码似乎没有将ajaxForm对象附加回替换div中存在的表单。这意味着我第二次使用表单发布时,会将用户重定向到页面之外。我想说这是控制器中的缓存问题,但我得到的响应显示了ascx中更新的项目列表。最后一件事,当我看到IE开发工具栏中的dom元素时,我看到一个属性如“jQuery16404440065521567182”,其值为33,并在第一次提交后消失。我猜这是由ajaxForm放在那里的。这是我正在使用的代码(一些javascript命名空间已更改为删除项目特定命名):
ASCX文件
<!-- Form to add a new record -->
<% using (Html.BeginForm("SaveAttachment", "Report", FormMethod.Post, new { enctype = "multipart/form-data", id = "AttachmentForm" })) { %>
<input type="hidden" name="ReportId" value="<%: Model.ReportId %>" />
<input type="file" name="FileUpload" value="" size="21" />
<label for="Title">
Title:</label>
<input type="text" name="Title" value="" />
<input type="submit" value="Add" class="inputButton" />
<% } %>
<!-- Display existing items -->
<% foreach (var item in Model.ExistingAttachments) { %>
<div>
<%: item.Sort %> <%: item.Title.PadRight(25, ' ').Substring(0, 25).TrimEnd() %></div>
<% } %>
ASPX文件
<div id="AttachmentsWindow">
<% Html.RenderPartial("LoadAttachments", Model.ReportAttachments); %>
</div>
<!-- This form is used to refresh the above div -->
<% using (Ajax.BeginForm("LoadAttachments", new { ReportId = Model.ReportId },
new AjaxOptions {
HttpMethod = "Post",
LoadingElementId = "LoadingAttachments",
UpdateTargetId = "AttachmentsWindow",
InsertionMode = InsertionMode.Replace,
OnComplete = "Report.InitAttachment"
}, new { id = "LoadAttachmentsForm" })) { %>
<input type="submit" name="submit" value="" class="button" style="float:right;"
onmouseover="this.className='button buttonhov'" onmouseout="this.className='button'"/>
<% } %>
控制器
[HttpPost]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public FileUploadJsonResult SaveAttachment(ReportAttachmentViewModel Attachment) {
if ( Attachment.FileUpload.ContentLength < 1 ){
return new FileUploadJsonResult { Data = new { message = "No file chosen." } };
}
var Report = this._reportRepository.GetById(Attachment.ReportId);
if (Report == null)
throw new Exception("Report not found");
MemoryStream target = new MemoryStream();
Attachment.FileUpload.InputStream.CopyTo(target);
byte[] data = target.ToArray();
ReportAttachment newobj = new ReportAttachment {
Attachment = data,
Description = string.Empty,
Name = Attachment.Title,
Report = Report,
ReportId = Report.Id
};
var result = this._reportAttachmentRepository.Add(ref newobj);
Report.ReportAttachments.Add(newobj);
ModelState.Clear();
if (!result.Success) {
StringBuilder sb = new StringBuilder();
foreach (var msg in result.Messages) {
sb.AppendLine(string.Format("{0}", msg.Message));
}
return new FileUploadJsonResult { Data = new { message = sb.ToString() } };
}
return new FileUploadJsonResult { Data = new { message = string.Format("{0} uploaded successfully.", System.IO.Path.GetFileName(Attachment.FileUpload.FileName)) } };
的Javascript
//Report namespace
InitAttachment: function () {
jQuery('#AttachmentForm').ajaxForm({
iframe: true,
dataType: "json",
beforeSubmit: function () {
jQuery('#AttachmentForm').block({ message: 'Uploading File... ' });
},
success: function (result) {
jQuery('#AttachmentForm').unblock();
jQuery('#AttachmentForm').resetForm();
$.growlUI(null, result.message);
Editor.EndLoading(false, false, true);
},
error: function (xhr, textStatus, errorThrown) {
$("#ajaxUploadForm").unblock();
$("#ajaxUploadForm").resetForm();
$.growlUI(null, 'Error uploading file');
}
});
//Editor namespace
EndLoading: function (ReloadReportSections, ReloadReferences, ReloadAttachments) {
//Reload sections
if (ReloadReportSections)
jQuery('#LoadReportSectionsForm').submit();
if (ReloadReferences)
jQuery('#LoadReferencesForm').submit();
if (ReloadAttachments) {
jQuery('#LoadAttachmentsForm').submit();
}
//endReload
Report.InitAttachment();
//Close the loading dialog
jQuery('#LoadingWindow').dialog('close');
}
答案 0 :(得分:0)
您可能希望在调用iframe
之前删除InitAttachment()
中隐藏的ajaxForm()
(如果存在)。
如果上一次上传的iframe
仍然存在,则听起来文件上传表单的目标可能会搞砸。
我在ExtJS
中遇到了类似的问题,但不能说它是相同的。
答案 1 :(得分:0)
事实证明,我在错误的位置调用更新。在我看来,我会想象OnSuccess
会在OnComplete
之前触发,但在MS Ajax模型中它不会。 OnSuccess
被调用,看起来它正在影响代码,因为它击中了我设置的所有断点,但它必须在调用Replace逻辑之前击中旧的DOM元素。我似乎无法找到任何文档来讨论发生这种情况的顺序,但我注意到当我从.ajaxForm
属性中调用AjaxOptions.OnSuccess
时,一切正常。所以我的建议简短:
如果您需要临时逻辑,请使用AjaxOptions.OnSuccess
和AjaxOptions.OnFailed
来影响更新后的DOM,AjaxOptions.OnComplete
。