[0523] TIL 28์ผ์ฐจ

nikevapormaxยท2022๋…„ 5์›” 23์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
27/116
post-custom-banner

๐Ÿ˜‚ ๋จธ์‹ ๋Ÿฌ๋‹ ํŒ€ ํ”„๋กœ์ ํŠธ

๐Ÿ˜ญ ์‚ฌ์ง„ ์—…๋กœ๋“œ ๋ฐ ๊ฒŒ์‹œ

  • ์šฐ๋ฆฌ ํŒ€์ด ์ •ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ์ž๊ฐ€ ๋ฌผ๊ณ ๊ธฐ์˜ ์‚ฌ์ง„์„ ์—…๋กœ๋“œํ•˜๊ฒŒ ๋˜๋ฉด, ํ•ด๋‹น ์‚ฌ์ง„์„ db์— ์ €์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์‹œ์— ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค.
  • ์‚ฌ์ง„์˜ ๊ฒฝ๋กœ๋ฅผ db์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ์ด๋ฏธ ์ €๋ฒˆ ์ฃผ์— ๋งˆ๋ฌด๋ฆฌํ•˜์˜€์ง€๋งŒ, ํ•ด๋‹น ์‚ฌ์ง„์„ ์ €์žฅํ•˜๋Š” ๋™์‹œ์— ์‚ฌ์šฉ์ž์—๊ฒŒ ์ œ์‹œํ•˜๋Š” ๊ฒƒ์—์„œ ๋งŽ์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.
  • ๋ฌธ์ œ์ 
      1. ajax ์ฝœ์„ ํ•˜๊ฒŒ ๋˜๋ฉด Live Server๊ฐ€ ์ž๋™์œผ๋กœ ์ƒˆ๋กœ๊ณ ์นจ๋˜์—ˆ๋‹ค.
      • ์šฐ๋ฆฌ๋Š” ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ VS Code๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. Live Server๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด์„œ ์œ„์˜ ๋ฌธ์ œ์ ์ด ๋ฐœ์ƒํ•˜์˜€๊ณ , ์—„์ฒญ๋‚œ ์‹œ๊ฐ„์„ ๋“ค์—ฌ ๊ตฌ๊ธ€๋ง์„ ํ•˜๊ณ  ์ง€์ธ์ฐฌ์Šค๋„ ์จ๋ดค์ง€๋งŒ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.
      • ์˜ค๋Š˜ ์ค€ํƒœ ํŠœํ„ฐ๋‹˜๊ป˜ ์—ฌ์ญค๋ดค๋Š”๋ฐ Live Server์˜ ์„ค์ •์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ์…จ๋‹ค.
        ํ™•์žฅ - ๋ผ์ด๋ธŒ ์„œ๋ฒ„ - ํ™•์žฅ ์„ค์ • - settings.json์—์„œ ํŽธ์ง‘
        ๋งจ ์•„๋žซ์ค„์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.
      • ํ•ด๋‹น ์ž‘์—…์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ์ƒˆ๋กœ๊ณ ์นจ๋˜๋Š” ํ˜„์ƒ์„ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.
      1. ๊ฒฝ๋กœ ์„ค์ •์—์„œ์˜ ๋ฌธ์ œ์ 
      • ์ด์ „๊นŒ์ง€ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ, ๊ทธ๋•Œ๋„ db์—๋Š” ์ €์žฅ์ด ์ˆ˜์›”ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์ง„ ์ €์žฅ์ด ๋˜์ง€ ์•Š์•„ ์ƒ๋Œ€๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์•˜๋‹ค.
      • ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์•˜๋‹ค.
@app.route("/upload", methods=['POST'])
@authorize
def upload_image(user):

    image = request.files['image_give']

    extension = image.filename.split('.')[-1]
    today = datetime.now()
    mytime = today.strftime('%Y%m%d%H%M%S')

    filename = f'{mytime}'

    save_to = f'backend/fish/{filename}.{extension}'

    image.save(save_to)
    save_to = "../" + save_to

    user = user['id']

    doc = {
        'image': save_to,
        'user_id': user
    }
    db.image.insert_one(doc)
    return jsonify({'result': 'success', 'user_id': user, 'save_to': save_to})

๊ฒฝ๋กœ ์ˆ˜์ • ํ›„, db์—๋„ ํŒŒ์ผ์˜ ์ด๋ฆ„์ด ์ž˜ ๋“ค์–ด๊ฐ€๊ณ , ์šฐ๋ฆฌ๊ฐ€ ๊ทธํ† ๋ก ์›ํ–ˆ๋˜ ํ”„๋ฆฌ๋ทฐ ๊ธฐ๋Šฅ๋„ img ํƒœ๊ทธ์˜ src๋ฅผ ํ†ตํ•ด ์ž˜ ์ ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

profile
https://github.com/nikevapormax
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€