I want to save the file and notes in backend using ajax in django. For file i am getting trouble. How to send the file to backend. Notes i am getting to view but i am not able to send file to view.
我想在django中使用ajax保存后端的文件和注释。对于文件我遇到了麻烦。如何将文件发送到后端。笔记我正在查看,但我无法发送文件进行查看。
<form class="horizontal-form" role="form" action="." method="post" enctype="multipart/form-data" id="nofilesforms">
<input type="file" name="image_file" id="image_file_data">
<textarea class="form-control notesdata" data-stylesheet-url="" name="notes"></textarea>
<button class="btn btn-primary" id="filesandnotes" type="button">Submit</button>
</form>
$(document).on('click','#filesandnotes',function(){
var notes = $('.notesdata').val();
var path = $('input[type=file][name=image_file]').val();
alert(path);
var data = {
notes:notes,
path:path
};
$.ajax({
url:"/cbs/msw1/",
type: "POST",
data: data,
success: function (response) {
},
error: function () {
}
});
});
Please anyone can guide me for this. Thanks a lot
请有人指导我这个。非常感谢
2 个解决方案
#1
0
First you have to bind on submit
of the form itself and not the click
event, prevent the default submit, get the file data (filename, filesize, blob, etc.) here is the link to the File
API, also the file input can hold multiple files, so you have to loop on the array of Files
. After that you send an AJAX request to your Django backend. And check it and do whatever you want. You also can use JS to check your form, but always check on the backend !
首先你必须绑定提交表单本身而不是click事件,阻止默认提交,获取文件数据(filename,filesize,blob等)这里是File API的链接,也可以输入文件保存多个文件,因此您必须循环文件数组。之后,您向Django后端发送一个AJAX请求。检查它,做你想做的任何事情。您也可以使用JS来检查您的表单,但始终检查后端!
#2
0
You'd better try this:
你最好试试这个:
$('#nofilesforms').submit(function(event) { ... });
Also, you may like to use a jQuery File Upload plugin: https://github.com/blueimp/jQuery-File-Upload/wiki/Submit-files-asynchronously
此外,您可能希望使用jQuery文件上载插件:https://github.com/blueimp/jQuery-File-Upload/wiki/Submit-files-asynchronously
#1
0
First you have to bind on submit
of the form itself and not the click
event, prevent the default submit, get the file data (filename, filesize, blob, etc.) here is the link to the File
API, also the file input can hold multiple files, so you have to loop on the array of Files
. After that you send an AJAX request to your Django backend. And check it and do whatever you want. You also can use JS to check your form, but always check on the backend !
首先你必须绑定提交表单本身而不是click事件,阻止默认提交,获取文件数据(filename,filesize,blob等)这里是File API的链接,也可以输入文件保存多个文件,因此您必须循环文件数组。之后,您向Django后端发送一个AJAX请求。检查它,做你想做的任何事情。您也可以使用JS来检查您的表单,但始终检查后端!
#2
0
You'd better try this:
你最好试试这个:
$('#nofilesforms').submit(function(event) { ... });
Also, you may like to use a jQuery File Upload plugin: https://github.com/blueimp/jQuery-File-Upload/wiki/Submit-files-asynchronously
此外,您可能希望使用jQuery文件上载插件:https://github.com/blueimp/jQuery-File-Upload/wiki/Submit-files-asynchronously