vue.jsから文字列と画像のファイルオブジェクトを引数にFlask API(GAE上でホスト)にアクセスし、Flask側から画像をストレージに保存、保存後にクライアント側(Vue)に画像のURL(文字列)を返す処理を作成しています。
画像の保存までは実装できているのですが、画像のURLを返すところでVue側のresponse.dataの値がundefinedになります。
headers情報などの記述が操作したいデータ型と一致してないことで生じてる問題かと思っているのですが、私自身がVue.jsでの経験が浅く、なかなか解決の糸口を掴めずにいます。
大変恐縮ではありますが、何かご意見を頂けると幸いでございます。
画像の通りこちらAPIへのPostリクエストはエラーコード200で処理自体は問題がないようです。
なので、Flaskのreturn値を取得できれば思いの挙動なのですが、、、、
uploadFile1: function () { var img_file1 = this.$refs.img1.files[0] this.imgFile1 = URL.createObjectURL(img_file1) var params = new FormData() params.append('image', img_file1) params.append('client_name', this.tableSelected) axios.post("API URL", params,{ withCredentials: true }, { headers: { 'Content-Type': 'text/plain*', }, }).then(response =>{ console.log(response.data)→undefined this.imgUrl1 = response.data }).catch(function (error) { console.log(error.message) for(let key of Object.keys(error)) { console.log(key); console.log(error[key]); } }); },
from flask import Flask, request, Response from google.cloud import storage as gcs from google.oauth2 import service_account from flask_cors import CORS import random, string app = Flask(__name__) CORS(app) @app.route('/', methods=['post']) def upload_img(): if request.files.get('image'): clientName = request.form.get('client_name') uploadFile = request.files.get('image') key_path = 'xxxxxxxxxx' randlst_a = [random.choice(string.ascdii_letters + string.digits) for i in range(10)] radians_b = ''.join(randlst_a) credential = service_account.Credentials.from_service_account_file(key_path) project_id = "xxxxxxxxxxxx" bucket_name = "フォルダー名" fileName = str(clientName) + radians_b + ".jpg" gcs_path = "img/{0}/{1}".format(clientName,fileName) client = gcs.Client(project_id, credentials=credential) client = gcs.Client(project_id) bucket = client.get_bucket(bucket_name) blob_gcs = bucket.blob(gcs_path) blob_gcs.upload_from_string(uploadFile.read(), content_type=uploadFile.content_type) imageURL = "https://storage.googleapis.com/dashboard_chichat/img/{0}/{1}".format(clientName,fileName) return imageURL if __name__ == "__main__": app.run(debug=True)
あなたの回答
tips
プレビュー