亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 io 中提取文件名。ReadCloser

從 io 中提取文件名。ReadCloser

Go
慕的地6264312 2022-09-05 17:37:08
我需要獲取從前端接收后端的某些文件的文件名。后端(在 Go 中實現)將以 .有沒有辦法從中提取它?io.ReadCloserio.ReadCloser
查看完整描述

5 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

后端(在 Go 中實現)將以 io 的形式接收文件。ReadCloser.有沒有辦法從io中提取它。ReadCloser?

不。

看看io的方法。ReadCloser 通過運行提供,并注意沒有一個方法可以提供名稱。所以除非你什么都不知道,它是一個io。ReadCloser 你根本做不到。go doc io.ReadCloser


查看完整回答
反對 回復 2022-09-05
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

package main


import (

    "errors"

    "fmt"

    "io"

    "os"

)


func fatalln(err error) {

    fmt.Fprintln(os.Stderr, err)

    os.Exit(1)

}


// hasName interface is an interface that expects types

// that implements it to have "Name() string" method.

type hasName interface {

    Name() string

}


func open(name string) (io.ReadCloser, error) {

    f, err := os.Open(name)

    if err != nil {

        return nil, err

    }

    // f implements io.ReadCloser interface as *os.File

    // has Read and Close methods.

    return f, nil

}


func main() {

    // rc is of the type io.ReadCloser

    rc, err := open("example.txt")

    if err != nil {

        fatalln(err)

    }

    defer rc.Close()


    // Type assetion to check rc's underlying type has

    // a method "Name() string".

    f, ok := rc.(hasName)

    if !ok {

        fatalln(errors.New("type assertion failed"))

    }


    // Yay, type assertion succeeded. Print the name!

    fmt.Println("Name:", f.Name())

}


查看完整回答
反對 回復 2022-09-05
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

通過定義嵌入的接口,您可以預先需要一個方法:io.ReaderName()


package main


import (

    "fmt"

    "io"

    "log"

    "os"

)


type NamedReadCloser interface {

    io.ReadCloser

    Name() string

}


func doThings(f NamedReadCloser) error {

    defer f.Close()

    b, err := io.ReadAll(f)

    if err != nil {

        return err

    }

    fmt.Printf("Name: %s, Content: %s\n", f.Name(), b)

    return nil

}


func main() {

    f, err := os.Open("/etc/hosts")

    if err != nil {

        log.Fatal("Cannot open file: ", err)

    }

    err = doThings(f)

    if err != nil {

        log.Fatal("Error doing things: ", err)

    }

}

僅當傳入的內容具有 name 方法(如 .如果沒有,那么您試圖做的事情是不可能的。*os.File


查看完整回答
反對 回復 2022-09-05
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

這是運行時讀取器的讀取器,當前端將文件發送到后端時,它從網絡讀取文件。您必須根據請求本身進行工作才能獲得該文件名。這是一個假設,但在大多數情況下,對于文件上傳,請求是多部分請求。如果您有相同的情況,則可以讀取標頭,通常用于標識文件類型。Go Native具有解析細節的能力。你可以試試這個 :io.ReadCloserContent-Dispositionhttp.Request


formFile, handler, err := r.FormFile("file")  // read file from network with key "file"

defer formFile.Close()


fileName := handler.Filename // Get file name


查看完整回答
反對 回復 2022-09-05
?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

您必須使用方法將其轉換為類型:Name


package main


import (

   "io"

   "os"

)


func open(name string) (io.ReadCloser, error) {

   return os.Open(name)

}


func main() {

   c, e := open("file.txt")

   if e != nil {

      panic(e)

   }

   defer c.Close()

   f := c.(*os.File)

   println(f.Name())

}


查看完整回答
反對 回復 2022-09-05
  • 5 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號