i创建泽西岛REST服务器并接收文件上传。这是i Follwed的教程:https://gist.github.com/aitoroses/4f7a2b197b732a6a691d
当我尝试使用Postman测试服务器时,一切正常。我选择了身体,form-data,键:文件,值:我的图片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上传文件吗?我尝试进行一些搜索,但我可以找到这样做的方法
$(...).val()
不会从文件输入中获取文件,它只是返回文件的名称。您需要执行$("#imgFile")[0].files[0]
并使用FormData发送文件:
var data = new FormData();
data.append('file', $("#imgFile")[0].files[0]);
$.ajax({ data: data, ... });