2017-08-16 109 views
0

我在服務器端使用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') 
+0

'disableImageResize'基本上是爲中間件不調整圖像大小(與imageMagik),所以可能無關你的問題。 – MrE

+0

如果沒有文件和其他內容,難以調試......但我會研究'#從Content-Range頭字符串中提取起始字節 range_str = request.headers ['Content-Range'] start_bytes = int(range_str。 split('')[1] .split(' - ')[0])'你在哪裏得到你的偏移量。也許這並不總是正確的。 – MrE

+1

也,你打開文件的標誌'a',你嘗試'ab'的二進制附加?因爲你得到的內容可能是二進制的,除非它是文本,你應該檢查編碼類型等...... – MrE

回答

2

不知道這是否是問題,但會嘗試

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

打開&追加到文件以二進制模式。