Skip to content

Instantly share code, notes, and snippets.

@johnsonz
Created August 9, 2017 04:00
Show Gist options
  • Save johnsonz/42b71eb86c2e87d38fb85cd73c473fb8 to your computer and use it in GitHub Desktop.
Save johnsonz/42b71eb86c2e87d38fb85cd73c473fb8 to your computer and use it in GitHub Desktop.
//form方式
<form id="uploadForm" enctype="multipart/form-data">
<input id="file" type="file" name="file"/>
<button id="upload" type="button">upload</button>
</form>
$.ajax({
url: '/upload',
type: 'POST',
cache: false,
data: new FormData($('#uploadForm')[0]),
processData: false,
contentType: false
}).done(function(res) {
}).fail(function(res) {});
//非form方式
<div id="uploadForm">
<input id="file" type="file"/>
<button id="upload" type="button">upload</button>
</div>
var formData = new FormData();
formData.append('file', $('#file')[0].files[0]);
$.ajax({
url: '/upload',
type: 'POST',
cache: false,
data: formData,
processData: false,
contentType: false
}).done(function(res) {
}).fail(function(res) {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment