Infinity8 Infinity8 - 6 months ago 45
Python Question

Chunked File Uploads have different file size/corrupt

I'm using jquery-file-upload and Python-Flask on the server side. Whenever I upload a large 100mb+ file, the uploaded version is slightly larger than the original and does not open (is corrupt). I have chunking enabled for large files at 10mb chunks, I've tried to set "disableImageResize" to "true" as well as tried single and multiple files and the result has been the same. Am I missing something in my code?

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')

MrE MrE
Answer Source

not sure if this is the issue, but would try

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

to open & append to the file in binary mode.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download