I have a problem when I try to upload files with Ajax in Django. I'm using FormData on Javascript. But the problem is in the server side. The files arrive to request.POST as dataURL, it should arrive to request.FILES, but it's empty.

This is my code:

var form_data = new FormData(document.getElementById("form-theme-edit")),
    url = "/theme/edit/1";

$.ajax({
    "url" : url,
    "method" : "POST",
    "enctype": "multipart/form-data",
    "data" : form_data,
    "processData" : false
});
有帮助吗?

解决方案

You need to set the contentType option to false to prevent jQuery from attempting to coerce the FormData object into a URL-encoded string when it constructs the request payload. Also, enctype is not a valid $.ajax option, so you should remove that.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top