0

我有下一个问题。当我发送带有表单的 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>

然后在服务器上我有代码:

print request.FILES

服务器打印:

<MultiValueDict: {u'upload': [<InMemoryUploadedFile: ID_12241.zip (application/zip)>]}>

当我在 jQuery 代码中逐步执行此操作时:

var content = zip.generate();
var options = {
    url: '/theme/zip/type/',
    data: {
       file: content
    }
};
$('#uploadForm').ajaxSubmit(options);

我在“request.POST”的“文件”参数中有这个文件,但“request.FILES”是空的()。我做错了什么?

4

2 回答 2

1

看看JQUery Form Plugin,它能够使用 ajax 上传文件。

根据他们网站上的文档

Browsers that support the XMLHttpRequest Level 2 will be able to upload files seamlessly and even get progress updates as the upload proceeds. For older browsers, a fallback technology is used which involves iframes since it is not possible to upload files using the level 1 implmenentation of the XMLHttpRequest object.

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

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

<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>

在 js 文件中添加以下代码:-

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.'); 
} 

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

  1. showRequest将在表单即将提交时调用,在这里您可以看到所有的发布数据。
  2. showResponse将在服务器返回响应时调用。

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

于 2014-02-22T15:37:39.347 回答
0

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

通过“ajax”上传的常用方法是使用 Flash swf 来处理同一页面上的上传,或者使用具有不可见 1x1 iframe 目标的表单。

尝试

http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/

或尝试http://www.uploadify.com/

于 2014-02-22T14:23:58.057 回答