1 回答
TA贡献1789条经验 获得超8个赞
我是这样解决的:
# Generate the melody based on the seed
@require_http_methods(["POST"])
def generate(request):
# See if method was post
if request.method == "POST":
# Do some stuff
midi = mg.save_melody(melody)
try:
with open(midi, 'rb') as f:
file_data = f.read()
response = HttpResponse(file_data, content_type="audio/midi", status=200)
except IOError:
response = JsonResponse({"succes": False}, status=400)
return response
和 JavaScript:
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
// Create new request add token
const generateRequest = new XMLHttpRequest();
generateRequest.open('POST', '/generate');
generateRequest.setRequestHeader('X-CSRFToken', csrftoken);
generateRequest.responseType = 'blob';
generateRequest.onload = () => {
// Get response from server
console.log(generateRequest.response);
let objectURL = URL.createObjectURL(generateRequest.response);
document.getElementById('myVisualizer').src = objectURL;
document.getElementById('playerMidi').src = objectURL;
};
添加回答
举报