是否可以通过手动构造标头,使用多部分表单数据发送此表单的请求。另外,对于此文件,内容/mime类型是什么。当我创建这种形式的请求时,请求到达服务器,但在那里收到的数据为空。另外,我收到跨域请求的错误消息,因为它不是jsonp。作为一个post请求,有没有办法做到这一点。服务器端采用Java。
发布于 2017-07-18 03:43:47
对于跨域请求,您需要向服务器添加跨域资源共享(CORS)标头:类似于Access-Control-Allow-Origin: *
。不完全确定这将在Java服务器中的什么地方,但如果您之前已经为服务器设置了头,CORS头将与之一起。我不能完全确定是否要通过JavaScript发送原始二进制文件。也许其他人知道得更清楚。
发布于 2017-07-18 04:09:08
基本上,您只需执行xhr POST/PUT请求。但首先将文件放入一个输入。
以下是我在一个旧项目中找到的put示例:
const fileInput = document.getElementById('file-input')
const file = fileInput.files[0]
const xhr = new XMLHttpRequest()
xhr.onerror = (e) => {
console.log(e)
}
xhr.upload.addEventListener('progress', (e) => {
// handle notifications about upload progress: e.loaded / e.total
const { loaded, total } = e
console.log(`${loaded} / ${total}`)
}, false)
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status >= 200 && xhr.status <= 299) {
// upload completed
}
}
}
xhr.open('PUT', 'linktoupload', true)
xhr.setRequestHeader('Content-Type', 'application/octet-stream')
xhr.send(file)
https://stackoverflow.com/questions/45157028
复制