如何以Django形式发布Javascript文件?

时间:2018-08-11 03:24:16

标签: javascript ajax django xmlhttprequest

我整整一天都在为此苦苦挣扎。在将我的图像上传到服务器之前,我已经使用this要点来压缩客户端的图像文件。到目前为止,图像已被压缩,文件(?)“ blob”已创建。该“ blob”是我要上传到我的视图的内容,但是我不能将其设置为表单内的隐藏文件输入,因为不允许这样做(出于安全原因)。压缩之前,我只需要添加一个输入字段,提交表单,然后在我的视图中从request.FILES检索它即可。我将如何发布该Blob?我听说过Ajax和XHR,但是我是编程新手,对此一无所知。

#basic_upload.html

<input type="file" id="select">
<img id="preview">
<script src="/static/assets/js/ImageTools.js" type="text/javascript"></script>
<script>
document.getElementById('select').onchange = function(evt) {
    ImageTools.resize(this.files[0], {
        width: 320, // maximum width
        height: 240 // maximum height
    }, function(blob, didItResize) {
        // didItResize will be true if it managed to resize it, otherwise false (and will return the original file as 'blob')
        document.getElementById('preview').src = window.URL.createObjectURL(blob);
        // you can also now upload this blob using an XHR.
    });
};
</script>

关于我应该从这里做什么的任何提示和示例?提前非常感谢!

0 个答案:

没有答案