예제 #1
0
파일: upload.py 프로젝트: goodking-bq/zblog
def upload():
    if request.method == 'POST':
        datestr = str(int(time()))
        file = request.files.get('imgFile', None)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            fileType = filename.split('.')[-1]
            if fileType in img:
                savename = 'img' + datestr + '.' + fileType
                fileurl = '/img/' + savename
                fileType = 'img'
            else:
                savename = 'att' + datestr + '.' + fileType
                fileurl = '/att/' + savename
                fileType = 'att'
            try:
                file.save(UPLOAD_FOLDER + fileurl)
                url = "/uploads/" + savename
                upd = Uploads(file_name=savename,
                              file_url=fileurl,
                              use_url=request.base_url,
                              file_type=fileType,
                              upload_user=g.user.id)
                upd.upload_date = datetime.now()
                db.session.add(upd)
                db.session.commit()
                data = {'error': 0, 'url': url}
                return json.dumps(data)
            except Exception, ex:
                data = {'error': 1, 'message': 'Exception:%s' % (ex)}
                return json.dumps(data)
        else:
            data = {'error': 1, 'message': u'不支持的文件类型'}
            return json.dumps(data)