您的位置 首页 golang

Go语言Web编程,Request http server 上传文件

上一节学习了POST请求,Form、PostForm、MultipartForm,关于POST还有2个功能,一个是上传文件,1个是JSON文件。

先学习下上传文件。

1. 文件上传

Go语言Web编程,Request http server 上传文件

Go语言Web编程,Request http server 上传文件

代码解析:

func FormUpload(w http.ResponseWriter, r *http.Request) {

//ParseMultipartForm将请求体解析为multipart/form-data。

//解析整个请求,1024是指其文件部分的maxMemory字节1024字节存储在内存,剩余的部分存储在临时文件。

_ = r.ParseMultipartForm(1024)

//获取文件信息,其中name是上传文件时所用的”name”名,[0]代表是第1个文件,tmp是FileHeader的结构体.

tmp := r.MultipartForm.File[“name”][0]

//获取文件头

log.Println(tmp.Header)

//获取文件大小

log.Println(tmp.Size)

//获取文件名

log.Println(tmp.Filename)

//打开后,返回File。

file, _ := tmp.Open()

//读取file文件,返回字节集

bytes, _ := ioutil.ReadAll(file)

//将读取的内容输出到页面

_, _ = w.Write(bytes)

}

总结一下上传步骤:

(1)调用ParseMultipartForm,解析请求

(2) 从MultipartForm.File获取文件信息

(3)使用ioutil.ReadAll将文件读入,转成字节集

2. 文件上传方法2

采用FormFile方法

Go语言Web编程,Request http server 上传文件

FormFile和MultipartForm.File相似,但是不同的是,FormFile返回的是第一个文件。

执行结果

Go语言Web编程,Request http server 上传文件

Go语言Web编程,Request http server 上传文件

使用FormFile方法更加简便,并需要调用ParseMultipartForm进行解析,同时返回multipart.File、FileHeader和error。

因此如果一次只上传一个文件,用这个方法更贱简便。

文章来源:智云一二三科技

文章标题:Go语言Web编程,Request http server 上传文件

文章地址:https://www.zhihuclub.com/86437.shtml

关于作者: 智云科技

热门文章

评论已关闭

42条评论

  1. That day, in fact, no matter what Assan thought about it later, he didn t southflaortho

  2. For many men, the 50mg dose of Viagra will help provide relief from ED and improvement in sexual performance

  3. That means a smaller amount is needed to produce the same effect so tried Cialis 10mg and well I was able to get an erection pretty easily but it didn t last for long at all and was unable to finish the job

  4. 2022 Aug 2;11 15 4509. Letrozole works for PCOS through its primary mechanism of action, that is, it inhibits estrogen production by repressing the enzyme aromatase.

  5. I realize how fortunate I am to have 3 children, and that my 15 months, 18 months and 6 months are nothing compared to couples who have tried for years to have one baby.

  6. The association between expression of downstream activated proteins and known prognostic factors was tested using Fisher s exact test

  7. You can t do without surgery or any other medical treatment; it s like a necessary sledgehammer Are you certain about the source

  8. Prevention of bone loss after allogeneic stem cell transplantation by calcium, vitamin D, and sex hormone replacement with or without pamidronate

  9. Representative images of c Fos neurons in the amygdala following intradermal PBS, histamine, chloroquine, serotonin, PBS Tween, or capsaicin injection are shown in Figure 3 A

  10. com 20 E2 AD 90 20Viagra 20Macun 20 20Finasterida 20E 20Viagra viagra macun Dr Elisaf said Although most patients recover when they stop drinking cola and take potassium supplements, cola induced chronic hypokalaemia can make them more susceptible to potentially fatal complications, such as an irregular heartbeat

  11. varieties and their relation to some of their pomological and phytonutrient characteristics Monitor Closely 1 nifedipine will increase the level or effect of fesoterodine by affecting hepatic intestinal enzyme CYP3A4 metabolism

  12. C3CO CASODEX CEPRATER CICLAMIL CIPROFARMA CIPROPLEX 50 CIPROTERONA BIOTENK CIPROTERONA DELTA FARMA CIPROTERONA GLENMARK CIPROTERONA KILAB CIPROTERONA LAFEDAR CIPROTERONA MICROSULES 50MG CIPROTERONA TECHSPHERE DIANE 35 DIMALAN FINABAND GEPEPROSTIN HITORON KEBIRTERONA LADOGAL LIBERPROST MILEVA 35 OMNIGERIAT PURFILX RAFFOLUTIL TESS 35 ZINNIA

  13. Glycosylphosphatidylinositol anchored high density lipoprotein binding protein 1 GPIHBP1 stabilizes LPL on the endothelial surface

  14. Patients with right cerebral hemisphere stroke often present with neglect syndrome 500 mg Rituximab Injection is used in the treatment of non Hodgkin lymphoma NHL, rheumatoid arthritis, blood cancer chronic lymphocytic leukemia, granulomatosis with polyangiitis and microscopic polyangiitis

  15. CreERT2, tamoxifen inducible Cre recombinase; Ctrl, control; CX3CR1, CX3C chemokine receptor 1; D, days; DT, diphtheria toxin; Iba1, ionized calcium binding adaptor molecule 1; IP, intraperitoneal; Mo, months

  16. Seems like your pronunciation of George Fant is not correct Minor 1 dronedarone will increase the level or effect of clarithromycin by affecting hepatic intestinal enzyme CYP3A4 metabolism

  17. Cord blood contains blood- forming stem cells, which are potentially useful for treating diseases that require stem cell transplants also called bone marrow transplants, such as certain kinds of leukemia or lymphoma, aplastic anemia, severe sickle cell disease, and severe combined immunodeficiency Eligibility was determined during a screening visit

  18. For me, they aren t worth the risk when SARMs are more targeted and safer Feldman J, Habib N, Radosevich J, Dutt M

  19. Total RNA was extracted with innuPREP Micro RNA Kit Analytik Jena, Austria and RNA quality was verified on an Agilent 2100 bioanalyzer The prevalence of infertility in the United States as estimated by the current duration approach and a traditional constructed approach

  20. Diuretic Laxative aperient Help increase bile flow choleretic Help treat digestive disturbances dyspepsia Alterative to help relieve dermatological conditions Help stimulate appetite While all these mechanisms are promising, the fact that tamoxifen treatment can reduce markers of oxidative stress in humans warrants a Mechanisms grade of A Table 1

  21. To confirm pregnancy, you really do need a reliable home pregnancy test or to get a test from your doctor

  22. At this time, Liu Yun had already united between heaven and man, and began lack of sleep hypertension to nitroglycerin tablets for high blood pressure concoct alchemy

  23. 47, 53 55 There have also been cases of NSTI with LRINEC scores of 0 Sleep laboratory investigations on hypnotic properties of melatonin

  24. Nancy K Mello This led most of the experts to formulate the guidelines to cautious recommendations

  25. I ve been at 20 carbs day for a few weeks now New mechanisms of bacterial resistance to antimicrobial agents

  26. Finally, it seems varicoceles almost always have effects on testosterone production The PMNs 1 10 6 cells mL in PBS Dulbecco were incubated at 37 C for 30, 60 and 90 min with each o methyl catechol 50 Вµg mL in the presence and absence of TAM 14 Вµg mL

  27. These included superficial mucosal necrosis and capillary vascular degenerative change in a background of reactive or chemical gastropathy

  28. Other types of laser eye surgery we offer Claudin 5 has been identified as a major constituent of the TJs of CNS ECs Figure 2

  29. 1 Learning Mechanisms In Obsessive Compulsive Disorder Bias To Stimulus Response Habit Learning ARVO Abstracts

网站地图