【发布时间】:2019-02-11 13:34:07
【问题描述】:
我正在使用
Python 3.6.3
烧瓶==1.0.2
Flask-Cors==3.0.7
Flask-RESTful==0.3.7
用于制作一个API,用于使用post方法收集tiff图像并保存在本地。
api = Api(app)
CORS(app)
class CatchImage(Resource):
@cross_origin(supports_credentials=True)
def post(self):
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
if which_extenstion(filename) != "tiff":
return json.dumps({
"id": None,
"error": "Unsupported file type"
})
else:
unique_id, folder_path, save_path = get_unique_id(filename)
try:
file.save(save_path)
except FileNotFoundError:
LookupError("no uploads folder")
convert_jpg_save(save_path)
jpg_image_path = get_jpg_image_path(folder_path)
img = [
url_for("send_image", filename=image)
for image in jpg_image_path
]
return jsonify({
"filename ": file.filename,
"id": unique_id,
"urls": img,
"error": None
})
return json.dumps({"error": "no file"})
api.add_resource(CatchImage, '/api/sendimage')
我已经尝试过使用 Postman 的 API,它工作得很好。但是当我尝试从浏览器访问 API 时,我得到了这个
POST http://127.0.0.1:5000/api/sendimage 400(错误请求)
相同的代码如下,由 Postman 生成。
var form = new FormData();
form.append("file", "/home/blue/Templates/test.tiff");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://127.0.0.1:5000/api/sendimage",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"Postman-Token": "4fdcc138-5567-4c4d-8f7d-8967d45b3c2a"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我确实认为这与 CORS 或设置了一些我无法弄清楚的错误有关。
我想了解问题的可能原因,主要是可能的解决方案。提前感谢您的时间 - 如果我遗漏了任何内容,过分或过分强调特定点,请在 cmets 中告诉我。
【问题讨论】: