我在我的一个项目中做到了这一点,下面的代码对我有用。请根据需要对代码进行必要的修改。
我的表单按钮:
<form name="upload_img" enctype="multipart/form-data" id="upload_img"><input type="file" id="upload-image" name="upload-image"></input><button id="upload_image" type="button">Save</button></form>
我的JQuery / Ajax:
$('#upload_image').click(function(){ var form = new FormData(document.getElementById('upload_img')); //append files var file = document.getElementById('upload-image').files[0]; if (file) {form.append('upload-image', file); } $.ajax({ type: "POST", url: "URL", data: form, cache: false, contentType: false, //must, tell jQuery not to process the data processdata: false, //data: $("#upload_img").serialize(), success: function(data) { if(data == 1) $('#img_msg').html("Image Uploaded Successfully"); else $('#img_msg').html("Error While Image Uploading"); } }); //alert('names');});


