Python summarizes two ways to upload images to the server and save them

I. Introduction

Two ways to save images to the server:

1. Save the image to a fixed location on the server according to its URL

2. request.FILES.get("file")Obtain the uploaded image file from the request according to the method and save it to a fixed location on the server

2. Method

1. URL of the image

To save an image to a fixed location on the server based on its URL you can use the following sample code:

import requests

def save_image_from_url(url, save_path):
    try:
        response = requests.get(url, stream=True)
        response.raise_for_status()
        with open(save_path, "wb") as file:
            for chunk in response.iter_content(chunk_size=8192):
                file.write(chunk)
        print("图片保存成功!")
    except requests.exceptions.RequestException as e:
        print("图片下载失败:", str(e))

# 定义图片的 URL
image_url = "http://example.com/image.jpg"

# 定义要保存的文件路径
save_path = "/path/to/save/image.jpg"

# 调用保存图片函数
save_image_from_url(image_url, save_path)

In the above example, we defined a save_image_from_urlfunction called which receives as parameters the URL of the image and the path to the file to be saved. Inside the function, we use requests.getthe method to send a GET request to obtain the image data and process it in the form of a binary stream. Then, we write the obtained data to the file block by block. Finally, the function will output a message that the image was saved successfully.

By calling save_image_from_urlthe function, you can save the image at the specified URL to the specified path save_path. Be sure to image_urlreplace with the actual image URL and save_pathwith the path to the file you want to save.

2. File type pictures

request.FILES.get("file")Get the uploaded image file from the request according to the method and save it to a fixed location on the server. You can use the following sample code:

def save_uploaded_image(file, save_path):
    try:
        with open(save_path, "wb") as destination:
            for chunk in file.chunks():
                destination.write(chunk)
        print("图片保存成功!")
    except Exception as e:
        print("图片保存失败:", str(e))

# 获取上传的文件对象
uploaded_file = request.FILES.get("file")

# 定义要保存的文件路径
save_path = "/path/to/save/image.jpg"

# 调用保存图片函数
save_uploaded_image(uploaded_file, save_path)

In the above example, we defined a save_uploaded_imagefunction called which receives as parameters the uploaded file object and the path of the file to be saved. Inside the function, we use openthe function to open the target file in binary write mode save_path, and then traverse the block data of the file object and write to the file block by block. Finally, the function will output a message that the image was saved successfully.

By calling save_uploaded_imagethe function, and passing request.FILES.get("file")the obtained file object and the specified save path, you can save the uploaded image to the specified fixed location on the server. Make sure to save_pathreplace with the actual path where the file is saved.

Guess you like

Origin blog.csdn.net/xun527/article/details/132763904