分块文件上载具有不同的文件大小/损坏

时间:2017-08-16 17:42:01

标签: javascript python flask jquery-file-upload blueimp

我在服务器端使用jquery-file-upload和Python-Flask。每当我上传一个大的100mb +文件时,上传的版本比原来的略大,并且不会打开(已损坏)。我已经为10mb块的大文件启用了分块,我试图将“disableImageResize”设置为“true”以及尝试单个和多个文件,结果是一样的。我在代码中遗漏了什么吗?

main.js

$(function () {
'use strict';
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
    // Uncomment the following to send cross-domain cookies:
    //xhrFields: {withCredentials: true},
    url: 'rs_upload',
    disableImageResize: true,
    sequentialUploads: true,
   // redirect: 'home',
    done: function (e, data) {
        console.log("uploaded: " + data.files[0].name)

    }
    , maxChunkSize: 10000000, // 10 MB,
}).bind('fileuploadstop', function (e, data) {
    if (data.loaded == data.total){window.location.replace("rs_create")}
});

views.py

@app.route("/rs_upload", methods=["GET", "POST"])
def rs_upload():
if request.method == 'POST':
    files = request.files['file']
    fs = files
    handle_file(fs)
    fullpath = session.get('finalpath')
    if 'Content-Range' in request.headers:
        # extract starting byte from Content-Range header string
        range_str = request.headers['Content-Range']
        start_bytes = int(range_str.split(' ')[1].split('-')[0])

        # append chunk to the file on disk, or create new
        with open(fullpath, 'a') as f:
            f.seek(start_bytes)
            f.write(fs.stream.read())

    else:
        # this is not a chunked request, so just save the whole file
        fs.save(fullpath)

    return jsonify({"name": fs.filename,
                    "size": os.path.getsize(fullpath),
                    "url": 'uploads/' + fs.filename,
                    "thumbnail_url": None,
                    "delete_url": None,
                    "delete_type": None,})

return render_template('remote_sensing/upload.html')

1 个答案:

答案 0 :(得分:2)

不确定这是否是问题,但会尝试

with open(fullpath, 'ab') as f:

打开&以二进制模式附加到文件。