Home  >  Article  >  Backend Development  >  How to handle file names with different encodings in Golang file upload?

How to handle file names with different encodings in Golang file upload?

WBOY
WBOYOriginal
2024-06-02 14:09:57356browse

To handle differently encoded file names in Go file upload, you can use the following two solutions: using mime.Header and URL decoding. mime.Header stores the original encoding via the filename field, while URL decoding uses the PathUnescape function to parse the filename.

Golang 文件上传中如何处理不同编码的文件名?

How to handle file names with different encodings in Go file upload

In Go, handle file names with different encodings to It is important to ensure the correctness of the file upload process. The widespread use of different character sets and encoding schemes can cause special characters in file names to be misinterpreted, resulting in file saving or processing failure.

To handle differently encoded filenames, you can use the following workaround:

Use mime.Header

mime.Header provides a filename field that can be used to parse and save the uploaded file name. The filename field stores the filename in its original encoding. The following example demonstrates how to get the original encoding of an uploaded file name:

import "mime/multipart"
import "fmt"

func handleFileUpload(r *http.Request) {
    r.ParseMultipartForm(32 << 20) // 设置最大上传大小为 32 MB

    file, _, err := r.FormFile("file")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

    fname := r.Form.Get("file")
    fnameDec, err := mime.ParseMediaType(fname)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("Original filename:", fname)
    fmt.Println("Decoded filename:", fnameDec.Filename)
}

Using URL Decoding

URL decoding is another common method for parsing uploaded file names. The following example demonstrates how to use URL decoding to extract the original file name from fname:

import "net/url"
import "fmt"

func handleFileUpload(r *http.Request) {
    r.ParseMultipartForm(32 << 20)

    file, _, err := r.FormFile("file")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

    fname := r.Form.Get("file")
    fnameDec, err := url.PathUnescape(fname)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("Original filename:", fname)
    fmt.Println("Decoded filename:", fnameDec)
}

Practical case

The following is a complete example code, Used to demonstrate how to use mime.Header and URL decoding to handle differently encoded file names in Go file uploads:

package main

import (
    "fmt"
    "net/http"
    "mime/multipart"
    "url"
)

func handleFileUpload(w http.ResponseWriter, r *http.Request) {
    r.ParseMultipartForm(32 << 20)

    file, _, err := r.FormFile("file")
    if err != nil {
        fmt.Fprintln(w, "Error parsing multipart form:", err)
        return
    }
    defer file.Close()

    fname := r.Form.Get("file")
    fnameDec, err := mime.ParseMediaType(fname)
    if err != nil {
        fmt.Fprintf(w, "Error parsing MIME header: %s", err)
        return
    }

    fmt.Fprintf(w, "Original filename: %s\n", fname)
    fmt.Fprintf(w, "Decoded filename (mime.Header): %s\n", fnameDec.Filename)

    fnameDec, err = url.PathUnescape(fname)
    if err != nil {
        fmt.Fprintf(w, "Error unencoding URL: %s", err)
        return
    }
    fmt.Fprintf(w, "Decoded filename (URL): %s", fnameDec)
}

func main() {
    http.HandleFunc("/upload", handleFileUpload)
    http.ListenAndServe(":8080", nil)
}

I hope this article can help you handle it efficiently in Go file uploads Differently encoded file names.

The above is the detailed content of How to handle file names with different encodings in Golang file upload?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn