如何使用jQuery的序列化办文件上传 [英] how to do file upload using jquery serialization

查看:130
本文介绍了如何使用jQuery的序列化办文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个表格,我通过AJAX使用jQuery的序列化功能提交表单

So I have a form and I'm submitting the form through ajax using jquery serialization function

        serialized = $(Forms).serialize();

        $.ajax({

        type        : "POST",
        cache   : false,
        url     : "blah",
        data        : serialized,
        success: function(data) {

        }

但然后呢,如果窗体有一个<输入类型=文件> 字段....我怎么传递文件到表单中使用此AJAX序列化方法...打印$ _FILES无法正常输出

but then what if the form has an <input type="file"> field....how do I pass the file into the form using this ajax serialization method...printing $_FILES doesn't output anything

推荐答案

一个文件无法使用AJAX,因为你不能访问存储在客户端计算机上的文件的内容,并使用JavaScript它在发送请求上传。一个实现此的技术是使用内置页框隐藏。有一个很好的的jQuery插件的形式,让您AJAXify您的形式和它的支持文件上传的为好。因此,使用这个插件的code将简单地是这样的:

A file cannot be uploaded using AJAX because you cannot access the contents of a file stored on the client computer and send it in the request using javascript. One of the techniques to achieve this is to use hidden iframes. There's a nice jquery form plugin which allows you to AJAXify your forms and it supports file uploads as well. So using this plugin your code will simply look like this:

$(function() {
    $('#ifoftheform').ajaxForm(function(result) {
        alert('the form was successfully processed');
    });
});

该插件会自动订阅的护理提交活动的形式,取消默认提交,序列化的价值观,用正确的方法和处理文件上传域, ......

The plugin automatically takes care of subscribing to the submit event of the form, canceling the default submission, serializing the values, using the proper method and handle file upload fields, ...

这篇关于如何使用jQuery的序列化办文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆