我创建了泽西REST服务器并接收文件上传。 这是我参考的教程:https://gist.github.com/aitoroses/4f7a2b197b732a6a691d
当我尝试使用Postman测试服务器时,一切正常。我选择了Body,form-data,key:file,value:my picture jpeg
我尝试做的是使用表单html和jquery上传文件 这是我的表格
<form>
<input id="imgFile" type="file" accept="image/*" value="Add file">
<input id="buttonPostReview" class="buttonSend" type="submit" value="Submit">
</form>
这是我的jquery
var postFile = function(e) {
e.preventDefault();
var imgFile = $('#imgFile').val();
var upLoalImgUrl = endPointUrl + 'webresources/photo';
console.log(imgFile);
console.log('uploading..');
$.ajax({
type: "POST",
url: upLoalImgUrl,
data: imgFile,
async: false,
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (returnData) {
console.log(returnData);
alert("Data Uploaded: ");
}
});
}
但我收到错误415 - 不支持的媒体类型 你能告诉我如何使用jquery上传文件吗?我尝试做一些搜索,但我可以找到这样做的方法
答案 0 :(得分:-1)
$(...).val()
没有从文件输入中获取文件,它只返回文件的名称。您需要执行$("#imgFile")[0].files[0]
并使用FormData发送文件:
var data = new FormData();
data.append('file', $("#imgFile")[0].files[0]);
$.ajax({ data: data, ... });