【发布时间】:2021-03-18 10:01:44
【问题描述】:
我确信这看起来很基础,但这是我第一次尝试使用 fetch 将文件上传到 django,我不确定如何在 Django 中操作和访问 FormData。
这会造成很多不工作jpg 文件。如何解决?
HTML:
<div>
<textarea></textarea>
<input type="file" multiple></input>
<button onClick="dothis()" >Sound</button>
</div>
JS:
function dothis(){
let data = new FormData();
let input = event.target.previousElementSibling;
for (let file of input.files){
d.append( "hello", file);
}
fetch("/images", {
credentials: "include",
method: "POST",
mode: "same-origin",
headers: {
"Accept": "application/json",
"Content-Type": "application/json", // or "multipart/form-data"??
"X-CSRFToken": csrf
},
body: data
})
.then(response => response.json())
.then(result => {
console.log("Well?");
})
Django views.by:
def images(request):
print(request.body)
form = request.body
filename = 0
for ff in form:
filename += 1
with open(f"{filename}.jpg", "wb+") as f:
f.write(ff)
return JsonResponse(status=201)
【问题讨论】:
标签: javascript django file-io fetch