【问题标题】:Chunked File Uploads have different file size/corrupt分块文件上传具有不同的文件大小/损坏
【发布时间】:2017-08-16 17:42:01
【问题描述】:

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

【问题讨论】:

  • disableImageResize 基本上是中间件不调整图像大小(使用 imageMagik)所以可能与您的问题无关。
  • 如果没有文件和其他文件就很难调试......但我会调查# extract starting byte from Content-Range header string range_str = request.headers['Content-Range'] start_bytes = int(range_str.split(' ')[1].split('-')[0]),你在哪里得到你的偏移量。也许这并不总是正确的。
  • 另外,您打开带有a 标志的文件,您是否尝试ab 以二进制形式附加?因为你得到的内容可能是二进制的,除非它是文本,你应该检查编码类型等......
  • 看起来它只是标志“a”而不是“ab”二进制标志。谢谢。

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2020-06-18
    • 1970-01-01
    • 2021-10-23
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2018-07-05
    相关资源
    最近更新 更多