如何从formdata读取文件?

时间:2018-11-05 23:57:26

标签: javascript jquery form-data

我的代码中有以下表格,为了向客户发送电子邮件,需要提交以下表格:-

    <form  enctype="multipart/form-data" id="jobapplication">
              <div class="form-group">
              <input type="email" class="form-control" aria-describedby="email Help" placeholder="Email Id *" name="email" required> <!--Input field for email-->
              </div>
               <div class="form-group">
              <input type="text" class="form-control" aria-describedby="Name Help" placeholder="Name *" name="name" required>   <!--Input field for name-->
               </div>
                <div class="form-group">
              <input type="phone" class="form-control" aria-describedby="Phone Help" placeholder="Phone *" name="phone" required> <!--Input field for phone-->
               </div>
                <div class="form-group">
              <input type="file" class="form-control" aria-describedby="Resume Help" placeholder="Resume" name="resume" required>  <!--Input for selcting resume-->
               </div>
                 <div class="form-group">
              <input type="text" class="form-control in-position " aria-describedby="email Help"  name="position" readonly> <!--Displaying the position applied-->
                 </div>
                 <div class="form-group">
                     <div class="g-recaptcha" data-sitekey="6LcpH3YUAAAAABF4UFt9WoLsrgug0wZ3O8zK0t_9"></div>
                 </div>
               <div class="form-group text-center">
                  <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button> <!--the cancel button-->
                  <button type="sumbit" class="btn btn-primary">Apply</button>  <!--the apply button-->
                </div>
             </form>

和以下javascript代码使用FormData和Ajax读取formdata

$(document).ready(function(){
       $('#jobapplication').submit(function(e){
           e.preventDefault();
           var fr=$('#jobapplication')[0];
           var form=new FormData(fr);

           $.ajax({
              type:'POST',
              url:'apply',
              data:form,
              dataType:'json',
              cache: false,
            contentType: false,
            processData: false
           }
             )
          .done(function(data)
          {
                if(data.info=="success")
                    {
                        window.location.href='/end';
                    }
           })
           .fail(function(data)
             {
               alert(data.err);
           })
           .always(function(data)
            {
               $('input[name=name]').val('');
               $('input[name=email]').val('');
               $('input[name=phone]').val('');
               $('input[name=resume]').val('');   
               grecaptcha.reset();   
           });
       }); 
    });

但是在运行时出现以下错误:- 在警报对话框中未定义 任何机构都可以告诉我如何从此表单读取文件,然后使用它汇总数据吗?

0 个答案:

没有答案