【转载】前端上传文件,python作为后端接收并保存到本地--Tornado上传文件--分片上传文件--更换pip下载源

背景:在改造caffe自带demo时,增加了一个更新模型的功能,需要将用户训练好的caffemodel上传到服务器,并替换到已经存在的caffemodel文件,重新加载上传的caffemodel文件并运行。最终改动结果页面如下:

1、使用Tornado上传文件

在海洋生物识别的过程中有一个上传本地图片进行检测的功能,对应这个功能进行了修改,但是出现以下错误,基本意思是Tornado默认上传大小为100MB 如果超过该大小就会出现以下错误。这里可以通过修改max_buffer_size属性来改变默认的上传大小,但是这里会有风险,因为Tornado是通过单字符串读入内存中的。

'Content-Length too' long when uploading file using Tornado

错误解释:

Tornado has a configurable limit on upload size (defaulting to 10MB). You can increase the limit by 
passing max_buffer_size to the HTTPServer constructor (or Application.listen). However, since 
Tornado (version 3.1) reads the entire upload body into a single contiguous string in memory, it's 
dangerous to make the limit too high.

不过这里如果小于100MB 的小文件还是推荐使用的,代码可以参考caffedemo的,关键代码如下

前端

 <script type="text/javascript">
      $(document).ready(
        function(){
          $('#classifyfile').attr('disabled',true);
          $('#imagefile').change(
            function(){
              if ($(this).val()){
                $('#formupload').submit();
              }
            }
          );
        }
      );
    </script>
<body>
 <form id="formupload" class="form-inline" role="form" action="classify_upload" method="post" enctype="multipart/form-data">
        <div class="form-group">
          <label for="imagefile">检测本地图像:</label>
          <input type="file" name="imagefile" id="imagefile">
        </div>
        <!--<input type="submit" class="btn btn-primary" value="Classify File" id="classifyfile"></input>-->
      </form>
</body>

Python后端代码:

@app.route('/model_upload', methods=['POST'])
def model_upload():
    try:
        # We will save the file to disk for possible data collection.

        model_file = flask.request.files['model_file']

        filename_ = werkzeug.secure_filename(model_file.filename)
        filename = os.path.join(UPLOAD_MODEL_FOLDER, filename_)
        model_file.save(filename)
        #filename = filename
        #imagefile.save(filename)
        logging.info('Saving to %s.', filename)
        #image = exifutil.open_oriented_im(filename)
        return flask.render_template(
            'right_update.html', has_result=True,
            result=(True, '上传成功')
        )
        #start_from_terminal(app)
    except Exception as err:
        logging.info('Uploaded model open error: %s', err)
        return flask.render_template(
            'right_update.html', has_result=True,
            result=(False, 'Cannot open uploaded caffemodel.')
        )

2、使用分片上传文件

感谢博主提供的代码,十分有帮助。请参考 大文件分片传输

这里为了展示效果粘贴一下效果图

上传成功

上传错误

3、更换pip下载源

由于在使用pip install 下载一下包时候由于网络原因总是超时,这里解决有三种方法,分别是:

(1)更换下载源

(2)fq

(3)修改默认超时时间

这里参考了替换pip下载源

写入内容为:

[global]
timeout = 6000
index-url = https://pypi.douban.com/simple
trusted-host = pypi.douban.com

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值