Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery将文件发送到Django_Jquery_Python_Ajax_Django_Jszip - Fatal编程技术网

JQuery将文件发送到Django

JQuery将文件发送到Django,jquery,python,ajax,django,jszip,Jquery,Python,Ajax,Django,Jszip,我还有一个问题。 当我发送带有表格的POST请求时: <form id="uploadForm" action="/theme/zip/type/" method="post" enctype="multipart/form-data"> {% csrf_token %} <input id="fileInput" class="input-file" name="upload" type="file"> <input type="submi

我还有一个问题。 当我发送带有表格的POST请求时:

<form id="uploadForm" action="/theme/zip/type/" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input id="fileInput" class="input-file" name="upload" type="file">
    <input type="submit" value="Upload" />
</form>
服务器打印:

<MultiValueDict: {u'upload': [<InMemoryUploadedFile: ID_12241.zip (application/zip)>]}>
我在'request.POST'中的'file'参数中有此文件,但'request.FILES'为空()。
我做错了什么?

传统意义上的Ajax是XMLHttpRequest,它不允许您编码本地文件并将其发送到服务器

通过“ajax”方式进行上传的常见方法是使用Flash swf在同一页面上处理上传,或者使用目标为不可见1x1 iframe的表单

试一试

或者试试看,它可以使用ajax上传文件

根据其网站上的文件

支持XMLHttpRequest级别2的浏览器将能够无缝上传文件,甚至在上传过程中获得进度更新。对于较旧的浏览器,使用涉及iFrame的回退技术,因为无法使用XMLHttpRequest对象的1级实现上载文件。

我在我的项目中使用它,您不必为旧浏览器定义任何iframe。这个插件会自动为你做这件事。下面是实现ajax文件上传的示例代码-

HTML文件如下所示(下载并链接HTML文件中的jquery.form.js):-

单击submit按钮后,将调用submit()函数。此函数返回false,这对于防止浏览器回发非常重要。 在此函数中定义了2个回调函数

  • showRequest
    将在表单即将提交时调用,在这里您可以看到所有post数据
  • showResponse
    将在服务器返回响应时调用

  • 在服务器上,您将在request.FILES中获取数据。从服务器返回一个JSON响应,可以在
    showResponse
    函数中访问该响应。

    您不能使用Ajax发送文件。
    <MultiValueDict: {u'upload': [<InMemoryUploadedFile: ID_12241.zip (application/zip)>]}>
    
    var content = zip.generate();
    var options = {
        url: '/theme/zip/type/',
        data: {
           file: content
        }
    };
    $('#uploadForm').ajaxSubmit(options);
    
    <form id="uploadForm" method="post" action="/theme/zip/type/"> {% csrf_token %}
        <input type="file" id="fileInput" name="upload"/>
        <button type="submit">Upload</button>
    </form>
    
    <script src="jquery-ui.min.js"></script>
    <script src="jquery.form.js"></script>
    
    var options = {
        beforeSubmit:showRequest,  // pre-submit callback
        success:showResponse,  // post-submit callback
        resetForm: false        // reset the form after successful submit
    };
    
    //uploadForm is the name of your form
    $('#uploadForm').submit(function() {
        // inside event callbacks 'this' is the DOM element so we first
        // wrap it in a jQuery object and then invoke ajaxSubmit
    
        $(this).ajaxSubmit(options);
    
        // !!! Important !!!
        // always return false to prevent standard browser submit and 
        // page navigation return false;
    });
    
    // pre-submit callback 
    function showRequest(formData, jqForm, options) { 
        // formData is an array; here we use $.param to convert it 
        // to a string to display it but the form plugin does 
        // this for you automatically when it submits the data 
        var queryString = $.param(formData); 
    
        // jqForm is a jQuery object encapsulating the form element. 
        // To access the DOM element for the form do this: 
        // var formElement = jqForm[0]; 
    
        alert('About to submit: \n\n' + queryString); 
    
        // here we could return false to prevent the form from being submitted; 
        // returning anything other than false will allow the 
        // form submit to continue 
        return true; 
    } 
    
    // post-submit callback 
    function showResponse(responseText, statusText, xhr, $form)  { 
        // for normal html responses, the first argument to the success callback 
        // is the XMLHttpRequest object's responseText property 
    
        // if the ajaxSubmit method was passed an Options Object with the dataType 
        // property set to 'xml' then the first argument to the success callback 
        // is the XMLHttpRequest object's responseXML property 
    
        // if the ajaxSubmit method was passed an Options Object with the dataType 
        // property set to 'json' then the first argument to the success callback 
        // is the json data object returned by the server 
    
        alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
            '\n\noutput div should have been updated with the responseText.'); 
    }