我找到了解决此问题的方法。
<input id="input-photos" name="Photos" multiple type="file" class="file-loading">
- 在Javascript中定义一个全局变量:
var formData = new FormData();
- 追加上
filePreUpload
动作所选择的文件复制到formData
。
$('#input-photos').on('filebatchpreupload', function(event, data, previewId, index) {
\t var form = data.form, files = data.files, extra = data.extra,
\t \t response = data.response, reader = data.reader;
\t $.each(files, function (key, value) {
\t \t if(value != null){
\t \t \t formData.append("Photos", value, value.name);
\t \t }
\t }); });
- 在表单提交追加所有表单字段和通过Ajax后的形式。
$('#yourForm').submit(function() {
\t $('#input-photos').fileinput('upload');
\t var model_data = $("#yourForm").serializeArray();
\t $.each(model_data,function(key,input){
\t \t formData.append(input.name,input.value);
\t });
\t $.ajax({
\t \t url: "@Url.Action("Save", "Home")",
\t \t type: "POST",
\t \t datatype: "json",
\t \t data: formData,
\t \t processData: false, // tell jQuery not to process the data
\t \t contentType: false, // tell jQuery not to set contentType
\t \t success: (function (result){
\t \t \t window.location.href = '@Url.Action("Index", "Home")';
\t \t }),
\t \t error: function (xhr) {
\t \t \t alert("Error: " + xhr.statusText);
\t \t }
\t });
\t return false;
});
这个 – shorif2000
可悲的是没有任何更新:)以及它的人已经死去 –