2015-08-18 168 views
2

我使用这个插件中的引导上传存储在同一个表单提交按钮文件Krajee文件输入提交表单文件提交

http://plugins.krajee.com/file-input/

我的问题是 - 一)有没有方法或东西来检查是否有仍然没有上传的dropZone中的文件,并且在他提交表单之后通知用户他没有上传文件

b)是否有一种方法会触发上传当提交按钮被解雇?

现在看起来是这样的 - 如果我提交表单,它不会上传的文件和刚刚通过的形式,我必须手动单击上传文件,然后提交 也许你们当中有些人碰到这个问题的形式,因为我不由于文档不佳,能够自己弄清楚。

+0

这个 – shorif2000

+0

可悲的是没有任何更新:)以及它的人已经死去 –

回答

0

我找到了解决此问题的方法。

<input id="input-photos" name="Photos" multiple type="file" class="file-loading"> 
  1. 在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; 
     
    });