无需设置formData即可提交其他表单数据

我正在关注如何提交附加表单数据并且有一个正在提交附加表单数据的工作文件上载,即。 附加的textarea输入字段, description[] ,具有分块和多文件支持,无需在.fileupload()上设置formData:{ } 。 我的问题是我只能将字段作为数组提交, description[] ,而我无法想出一些逻辑来将适当的description[i]与适当的文件相关联。 我使用的是asp.net mvc,在这种情况下无关紧要。

风景:

  $(document).ready(function () { $('#btnContinue').prop('disabled', true); $('#fileupload').fileupload({ url: '@Url.Action("UploadChunk", "Upload")', maxChunkSize: 1048576, }).addClass('fileupload-processing') .bind('fileuploadalways', function (e, data) { if ($('#fileIds').val().indexOf(';' + data.result.id + ';') == -1) { var pre = $('#fileIds').val() == "" ? ';' : ''; var append = $('#fileIds').val() + pre + data.result.id + ';'; $('#fileIds').val(append); } }) .bind('fileuploaddone', function (e, data) { $('#btnContinue').prop('disabled', false); }); $.ajax({ url: $('#fileupload').fileupload('option', 'url'), dataType: 'json', context: $('#fileupload')[0] }).always(function () { $(this).removeClass('fileupload-processing'); }); });  @using (Html.BeginForm("FileSet", "Upload", FormMethod.Post, new { id = "fileupload" } )) { @Html.AntiForgeryToken() @Html.Hidden("fileIds", "", new { @id = "fileIds" }) 

@Html.ValidationSummary(true, "", new { @class = "text-danger" })
Add files...
 
} {% for (var i=0, file; file=o.files[i]; i++) { %}

{%=file.name%}

Processing...

{% if (!i && !o.options.autoUpload) { %} {% } %} {% if (!i) { %} {% } %} {% } %} {% for (var i=0, file; file=o.files[i]; i++) { %} {% if (file.thumbnailUrl) { %} {% } %}

{% if (file.url) { %} {%=file.name%} {% } else { %} {%=file.name%} {% } %}

{% if (file.error) { %}
Error {%=file.error%}
{% } %} {%=o.formatFileSize(file.size)%} {% if (file.deleteUrl) { %} {% } else { %} {% } %} {% } %}

控制者:

 [HttpPost] public ContentResult UploadChunk(string[] description) { ParseRequestHeaderRanges(this.Request); var r = new List(); var i = 0; foreach (string file in Request.Files) { HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength == 0) continue; var uploader = System.Security.Principal.WindowsIdentity.GetCurrent().Name; var result = DataTranslator.UploadChunk(hpf.InputStream, hpf.FileName, description[i], hpf.ContentType, hpf.ContentLength, (int)hpf.InputStream.Length, this.TotalBytes, uploader); r.Add(result); i++; } return Content("{\"id\":\"" + r[0].Id + "\",\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}", "application/json"); } 

哈希不是让它成为一个数组吗? 说使用文件名? 这样你就不必依赖于正确顺序的东西。

  

在控制器中:

 var result = DataTranslator.UploadChunk( hpf.InputStream, hpf.FileName, description[hpf.FileName], hpf.ContentType, hpf.ContentLength, (int)hpf.InputStream.Length, this.TotalBytes, uploader ); 
Interesting Posts