{"id":11052,"date":"2022-12-16T12:00:44","date_gmt":"2022-12-16T04:00:44","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=11052"},"modified":"2024-11-14T17:52:12","modified_gmt":"2024-11-14T09:52:12","slug":"upload","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2022\/12\/16\/upload\/","title":{"rendered":"\u901a\u8fc7gin\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20\u4e0e\u4e0b\u8f7d"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\uff0cmultipart\u901a\u5e38\u6307\u7684\u662fmultipart\/form-data\uff0c\u8fd9\u662f\u4e00\u79cd\u7528\u4e8eHTTP\u534f\u8bae\u7684MIME\u7c7b\u578b\uff0c\u7528\u4e8e\u5728HTTP\u8bf7\u6c42\u4e2d\u53d1\u9001\u5305\u542b\u591a\u4e2a\u90e8\u5206\u7684\u5b9e\u4f53\u5185\u5bb9\u3002\u8fd9\u79cd\u7c7b\u578b\u901a\u5e38\u7528\u4e8e\u5728HTML\u8868\u5355\u4e2d\u4e0a\u4f20\u6587\u4ef6\u6216\u63d0\u4ea4\u5305\u542b\u591a\u79cd\u7c7b\u578b\u6570\u636e\u7684\u8868\u5355\uff0cmultipart\/form-data\u662f\u57fa\u4e8ePOST\u8bf7\u6c42\u6765\u5b9e\u73b0\uff0c\u4e00\u822c\u7528\u4e8e\u4e0a\u4f20\u6587\u4ef6\uff0cgo\u8bed\u8a00\u7684\u5b98\u65b9\u5e93multipart\u4e2d\u4e5f\u5b9e\u73b0\u4e86\u5bf9multipart\/form-data\u7684\u8bf7\u6c42\uff0c\u4ece\u800c\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">multipart\/form-data\u5f62\u5f0f\u7684post\u4e0e\u666e\u901apost\u8bf7\u6c42\u7684\u4e0d\u540c\u4e4b\u5904\u4f53\u73b0\u5728\u8bf7\u6c42\u5934\uff0c\u8bf7\u6c42\u4f532\u4e2a\u90e8\u5206<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bf7\u6c42\u5934<\/strong>\uff1a\u5fc5\u987b\u5305\u542bContent-Type\u4fe1\u606f\uff0c\u4e14\u5176\u503c\u4e5f\u5fc5\u987b\u89c4\u5b9a\u4e3amultipart\/form-data\uff0c\u540c\u65f6\u8fd8\u9700\u8981\u89c4\u5b9a\u4e00\u4e2a\u5185\u5bb9\u5206\u5272\u7b26\u7528\u4e8e\u5206\u5272\u8bf7\u6c42\u4f53\u4e2d\u4e0d\u540c\u53c2\u6570\u7684\u5185\u5bb9(\u666e\u901apost\u8bf7\u6c42\u7684\u53c2\u6570\u5206\u5272\u7b26\u9ed8\u8ba4\u4e3a&amp;\uff0c\u53c2\u6570\u4e0e\u53c2\u6570\u503c\u7684\u5206\u9694\u7b26\u4e3a=)\uff0c\u5177\u4f53\u7684\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Content-Type: multipart\/form-data; boundary=${bound}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d${bound} \u662f\u4e00\u4e2a\u5360\u4f4d\u7b26\uff0c\u4ee3\u8868\u6211\u4eec\u89c4\u5b9a\u7684\u5177\u4f53\u5206\u5272\u7b26\uff1b\u53ef\u4ee5\u81ea\u5df1\u4efb\u610f\u89c4\u5b9a\uff0c\u4f46\u4e3a\u4e86\u907f\u514d\u548c\u6b63\u5e38\u6587\u672c\u91cd\u590d\u4e86\uff0c\u5c3d\u91cf\u8981\u4f7f\u7528\u590d\u6742\u4e00\u70b9\u7684\u5185\u5bb9,\u5982\uff1a&#8211;12345678<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bf7\u6c42\u4f53<\/strong>\uff1a\u5b83\u4e5f\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e0d\u8fc7\u548c\u666e\u901apost\u8bf7\u6c42\u4f53\u4e0d\u540c\u7684\u662f\u5b83\u7684\u6784\u9020\u65b9\u5f0f\u3002\u666e\u901apost\u8bf7\u6c42\u4f53\u662f\u7b80\u5355\u7684\u952e\u503c\u5bf9\u8fde\u63a5\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> a=b&amp;c=d&amp;e=f<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800cmultipart\/form-data\u5219\u662f\u6dfb\u52a0\u4e86\u5206\u9694\u7b26\u3001\u53c2\u6570\u63cf\u8ff0\u4fe1\u606f\u7b49\u5185\u5bb9\u7684\u6784\u9020\u4f53\u3002\u5177\u4f53\u683c\u5f0f\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--${bound}\nContent-Disposition: form-data; name=\"Filename\" \/\/\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u76f8\u5f53\u4e8ek1\uff1b\u7136\u540e\u56de\u8f66\uff1b\u7136\u540e\u662f\u53c2\u6570\u7684\u503c\uff0c\u5373v1\n \nHTTP.pdf \/\/\u53c2\u6570\u503cv1\n--${bound} \/\/\u5176\u5b9e${bound}\u5c31\u76f8\u5f53\u4e8e\u4e0a\u9762\u666e\u901apost\u8bf7\u6c42\u4f53\u4e2d\u7684&amp;\u7684\u4f5c\u7528\nContent-Disposition: form-data; name=\"file000\"; filename=\"HTTP\u534f\u8bae\u8be6\u89e3.pdf\" \/\/\u8fd9\u91cc\u8bf4\u660e\u4f20\u5165\u7684\u662f\u6587\u4ef6\uff0c\u4e0b\u9762\u662f\u6587\u4ef6\u63d0\nContent-Type: application\/octet-stream \/\/\u4f20\u5165\u6587\u4ef6\u7c7b\u578b\uff0c\u5982\u679c\u4f20\u5165\u7684\u662f.jpg\uff0c\u5219\u8fd9\u91cc\u4f1a\u662fimage\/jpeg\n \n%PDF-1.5\nfile content\n%%EOF\n--${bound}\nContent-Disposition: form-data; name=\"Upload\"\n \nSubmit Query\n--${bound}--<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u90fd\u662f\u4ee5${bound}\u4e3a\u5f00\u5934\u7684\uff0c\u5e76\u4e14\u6700\u540e\u4e00\u4e2a${bound}\u540e\u9762\u8981\u52a0&#8211;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u3001\u6e90\u7801\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gin\u6587\u4ef6\u4e0a\u4f20\u4e0e\u539f\u751f\u7684net\/http\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4e0d\u540c\u5728\u4e8egin\u628a\u539f\u751f\u7684request\u5c01\u88c5\u5230c.Request\u4e2d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u67e5\u770bgin Context\u4e2d\u5df2\u7ecf\u5d4c\u5957\u4e86\u539f\u751f\u7684http.Request\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"219\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/13-3.png\" alt=\"\" class=\"wp-image-11056\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/13-3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/13-3-300x93.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/13-3-600x185.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001Request\u4e2d\u7684FormFile\u65b9\u6cd5\u53ef\u63d0\u4f9b\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\uff0c\u53c2\u6570\u4e3akey\u7684\u540d\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"171\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/14-3.png\" alt=\"\" class=\"wp-image-11057\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/14-3.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/14-3-300x72.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/14-3-600x145.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5FormFile\u8fd4\u56de\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3amultipart\u5e93\u7684\u63a5\u53e3File\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"199\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/15-2.png\" alt=\"\" class=\"wp-image-11058\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/15-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/15-2-300x84.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/15-2-600x168.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">File\u5b9e\u73b0\u4e86\u5bf9\u4e00\u4e2amultipart\u4fe1\u606f\u4e2d\u6587\u4ef6\u8bb0\u5f55\u7684\u8bbf\u95ee\uff0c\u53ea\u80fd\u8bfb\u53d6\u6587\u4ef6\u800c\u4e0d\u80fd\u5199\u5165\u3002\u5b83\u7684\u5185\u5bb9\u53ef\u4ee5\u4fdd\u6301\u5728\u5185\u5b58\u6216\u8005\u786c\u76d8\u4e2d\uff0c\u5982\u679c\u4fdd\u6301\u5728\u786c\u76d8\u4e2d\uff0c\u5e95\u5c42\u7c7b\u578b\u5c31\u4f1a\u662f*os.File<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u63cf\u8ff0\u4e00\u4e2amultipart\u8bf7\u6c42\u7684\u6587\u4ef6\u8bb0\u5f55\u7684\u4fe1\u606f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"250\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/1-5.png\" alt=\"\" class=\"wp-image-11060\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/1-5.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/1-5-300x106.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/1-5-600x211.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">FileHeader\u4e2d\u5305\u542b\u4e86\u83b7\u53d6\u6587\u4ef6\u540d\u5b57\u6bb5\uff0c\u6587\u4ef6\u5927\u5c0f\u5b57\u6bb5\u4ee5\u53ca\u6587\u4ef6\u5934\u7684\u5b57\u6bb5\uff0c\u76f8\u5173\u7684\u65b9\u6cd5\u53ef\u53c2\u8003\u5b89\u88c5\u8def\u5f84\u4e0bsrc\/mime\/multipart\/formdata.go\u7684\u7528\u6cd5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u4e5f\u53ef\u4e0d\u4f7f\u7528http.Request\u4e2d\u7684FormFile\u65b9\u6cd5\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><strong>\u76f4\u63a5\u4f7f\u7528gin.Context\u7684FormFile<\/strong><\/mark>\u65b9\u6cd5\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"134\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/5-5.png\" alt=\"\" class=\"wp-image-11070\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/5-5.png 714w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/5-5-300x56.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/5-5-600x113.png 600w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">FormFile\u8fd4\u56de\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u5c31\u662fmultipart\u5e93\u7684FileHeader\u7ed3\u6784\u4f53\u6307\u9488\uff0c\u4e0e\u7b2c2\u6b65\u4e2d\u7684\u4e00\u6837\uff0c\u8fd8\u6709\u4e00\u4e2a\u9519\u8bef\u8fd4\u56de<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e8c\u3001\u5355\u6587\u4ef6\u4e0a\u4f20\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u901a\u8fc7\u539f\u751f\u7684http\u5305\u4e2d\u7684Request\u7684FormFile\u4e0a\u4f20<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5355\u4e2a<\/mark><\/strong>\u6587\u4ef6\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"664\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-5.png\" alt=\"\" class=\"wp-image-11063\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-5.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-5-300x281.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-5-600x561.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c20\u884c\uff1a\u901a\u8fc7gin\u5c01\u88c5\u7684Request\u8bf7\u6c42FormFile,\u5b9e\u9645\u662f\u8bbf\u95eehttp.Request\u4e2d\u7684FormFile\uff0c\u4e0a\u4f20\u7684\u6587\u4ef6\u7684key\u540d\u4e3aimage<\/li>\n\n\n\n<li>\u7b2c25\u884c\uff1a\u8c03\u7528\u51fd\u6570\u8fd4\u56de\u6587\u4ef6\u5927\u5c0f<\/li>\n\n\n\n<li>\u7b2c27\u884c\uff1a\u8c03\u7528\u51fd\u6570SaveUploadFile\u50a8\u5b58\u6587\u4ef6,\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3aFormFile\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u5b58\u50a8\u8def\u5f84+\u6587\u4ef6\u540d\uff0c\u5982\u679c\u8def\u5f84\u4e0d\u5b58\u5728\u4f1a\u62a5\u9519\uff0c\u56e0\u6b64\u5b58\u50a8\u4e4b\u524d\u9700\u8981\u5224\u65ad,\u672c\u4f8b\u5b50\u76f4\u63a5\u5199\u6b7b\u7684<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7postman\u53d1\u9001\u4e0a\u4f20\u6587\u4ef6\u8bf7\u6c42\uff0c\u4e0a\u4f20\u6587\u4ef6\u7684key\u4e3aimage\uff0c\u540d\u4e3a1.png\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"308\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-4.png\" alt=\"\" class=\"wp-image-11066\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-4.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-4-300x130.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-4-600x260.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fdb\u5165\/tmp\/linshi\u76ee\u5f55\u4e0b\u53ef\u4ee5\u770b\u5230\u5df2\u7ecf\u4fdd\u5b58\u5230\u673a\u5668\u4e0a\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"120\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-4.png\" alt=\"\" class=\"wp-image-11068\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-4.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-4-300x51.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-4-600x102.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u751f\u4ea7\u4e2d\u5f88\u5c11\u76f4\u63a5\u901a\u8fc7\u6587\u4ef6\u540d\u5b58\u50a8\uff0c\u53ef\u901a\u8fc7md5\u7b49\u65b9\u5f0f\u52a0\u5bc6\u540e\u5b58\u50a8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u76f4\u63a5\u901a\u8fc7gin.Context\u4e2d\u7684FormFile\u65b9\u6cd5\u5b9e\u73b0\u5355\u4e2a\u6587\u4ef6\u4e0a\u4f20\uff0c\u6dfb\u52a0\u4e86\u6253\u5370\u8bf7\u6c42\u5934\u4fe1\u606f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"616\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/1.png\" alt=\"\" class=\"wp-image-13328\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/1.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/1-300x261.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/1-600x522.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf7\u6c42\u7ed3\u679c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"318\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/4-2.png\" alt=\"\" class=\"wp-image-13330\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/4-2.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/4-2-300x135.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/4-2-600x269.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u770b\u78c1\u76d8\u76ee\u5f55\/tmp\/\u4e0b\u662f\u5426\u5b58\u5728\u6587\u4ef6\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"137\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/3-2.png\" alt=\"\" class=\"wp-image-13333\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/3-2.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/3-2-300x58.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/05\/3-2-600x116.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf7\u6c42\u5934\u53ef\u901a\u8fc7res.Header\u6765\u83b7\u53d6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bf7\u6c42\u5934\u4fe1\u606f: map&#91;Content-Disposition:&#91;form-data; name=\"image\"; filename=\"4.png\"] Content-Type:&#91;image\/png]]<\/code><\/pre>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u5982\u679c\u4f20\u5165png\u56fe\u7247\u90a3\u4e48Content-Type\u4f4d\u7f6e\u4f1a\u663e\u793aimage\/png\uff0c\u5982\u679c\u4f20\u5165jpg\u56fe\u7247\u5c06\u663e\u793aimage\/jpep\uff0c\u5982\u679c\u4f20\u5165\u6587\u4ef6\u5c06\u663e\u793aapplication\/\u6587\u4ef6\u540e\u7f00\u4fe1\u606f\uff0c\u603b\u4e4b\u6839\u636e\u4f20\u5165\u4e0d\u540c\u663e\u793a\u4e0d\u540c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e09\u3001\u591a\u6587\u4ef6\u4e0a\u4f20\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u6587\u4ef6\u4e0a\u4f20\u9700\u8981\u4f7f\u7528Context.MultipartForm\u65b9\u6cd5\uff0c\u8be5\u8bbf\u95ee\u5b9e\u73b0\u5bf9multiForm\u7684\u89e3\u6790\uff0c\u5e76\u53ef\u4ee5\u83b7\u5f97\u6587\u4ef6\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u6587\u4ef6\u5207\u7247\u5e76\u8c03\u7528SaveUploadFile\u4fdd\u5b58\u6587\u4ef6\uff0c\u67e5\u770b\u8be5\u65b9\u6cd5\u5185\u5bb9\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"146\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-5.png\" alt=\"\" class=\"wp-image-11089\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-5.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-5-300x62.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-5-600x123.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">MultipartForm\u65b9\u6cd5\u8fd4\u56de\u4e24\u4e2a\u53c2\u6570\uff0c\u6211\u4eec\u67e5\u770bmultipart\u7684\u7ed3\u6784\u4f53Form\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"212\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-6.png\" alt=\"\" class=\"wp-image-11090\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-6.png 711w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-6-300x89.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-6-600x179.png 600w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u6784\u4f53Form\u7684\u5b57\u6bb5\u7c7b\u578b\u90fd\u662fmap\u7c7b\u578b\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u6bb5File\u83b7\u53d6\u5c31\u662f\u4e0a\u4f20\u7684\u6587\u4ef6\u5185\u5bb9\uff0ckey\u4e3a\u4e0a\u4f20\u65f6\u6307\u5b9a\u7684\u540d\uff0c\u503c\u4e3amultipart.FileHeader\u7ed3\u6784\u4f53\u6307\u9488\u5207\u7247(\u4e0a\u65b9\u6709FileHeader\u8bf4\u660e)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u5b9a\u4e49\u591a\u6587\u4ef6\u4e0a\u4f20\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"772\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-7.png\" alt=\"\" class=\"wp-image-11099\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-7.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-7-276x300.png 276w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/4-7-551x600.png 551w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7postman\u8bf7\u6c42\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"317\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-7.png\" alt=\"\" class=\"wp-image-11096\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-7.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-7-300x134.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/2-7-600x268.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf7\u6c42\u540e\u67e5\u770b\u78c1\u76d8\u76ee\u5f55\/tmp\/linshi\uff0c\u5df2\u7ecf\u4e0a\u4f20\u6210\u529f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"137\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-6.png\" alt=\"\" class=\"wp-image-11097\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-6.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-6-300x58.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/3-6-600x116.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u5b9a\u4e49\u591a\u6587\u4ef6\u4e0a\u4f20\uff0c\u5e76\u5224\u65ad\u662f\u5426\u4e0a\u4f20\u4e86\u6587\u4ef6\u4ee5\u53ca\u6587\u4ef6\u5927\u5c0f\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"log\"\n\t\"net\/http\"\n\t\"path\"\n\n\t\"github.com\/gin-gonic\/gin\"\n)\n\nfunc main() {\n\trouter := gin.New()\n\trouter.POST(\"\/upload\", func(c *gin.Context) {\n\t\tmf, err := c.MultipartForm() \/\/\u5224\u65ad\u662f\u5426\u4e0a\u4f20\u4e86\u6587\u4ef6\n\t\tif len(mf.File) == 0 {\n\t\t\tginJSON(c, \"\u8bf7\u81f3\u5c11\u9009\u62e9\u4e00\u4e2a\u6587\u4ef6\u4e0a\u4f20\")\n\t\t}\n\t\tif err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tfor _, v := range mf.File&#91;\"file\"] {\n\t\t\tif v.Size &gt; 4194304 { \/\/\u5224\u65ad\u6587\u4ef6\u662f\u5426\u8fc7\u5927\n\t\t\t\tginJSON(c, \"\u6587\u4ef6\u8fc7\u5927,\u4e0d\u80fd\u8d85\u8fc74M,\u8bf7\u91cd\u65b0\u9009\u62e9!!\")\n\t\t\t}\n\t\t\tdst := path.Join(\"\/tmp\/test\", v.Filename)\n\t\t\terr := c.SaveUploadedFile(v, dst)\n\t\t\tif err != nil {\n\t\t\t\tlog.Fatal(err)\n\t\t\t}\n\t\t\tginJSON(c, \"\u6587\u4ef6\u4e0a\u4f20\u6210\u529f\")\n\t\t}\n\t})\n\trouter.Run()\n}\nfunc ginJSON(c *gin.Context, s string) {\n\tc.JSON(http.StatusOK, gin.H{\n\t\t\"code\": 1000,\n\t\t\"data\": s,\n\t})\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u56db\u3001\u6587\u4ef6\u4e0b\u8f7d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u4ef6\u4e0b\u8f7d\u53ef\u501f\u52a9GET()\u65b9\u6cd5\u4ee5\u53caFile()\u65b9\u6cd5\u6765\u5b9e\u73b0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u901a\u8fc7GET()\u63a5\u53e3\u4e0b\u8f7d\/tmp\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6a.txt\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n        \"fmt\"\n        \"net\/http\"\n        \"os\"\n\n        \"github.com\/gin-gonic\/gin\"\n)\n\nfunc main() {\n        router := gin.New()\n        \/\/\u5b9a\u4e49\u52a8\u6001\u8def\u7531\n        router.GET(\"\/getFile\/:filename\", func(c *gin.Context) {\n                f := c.Param(\"filename\")\n                filePath := \"\/tmp\/\" + f\n                _, err := os.Stat(filePath)\n                if os.IsNotExist(err) { \/\/\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\n                        c.String(http.StatusNotFound, \"file not found\")\n                        return\n                }\n                \/\/Content-Disposition:\u6307\u793a\u5ba2\u6237\u7aef\u5982\u4f55\u5904\u7406\u54cd\u5e94\u6b63\u6587\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u544a\u8bc9\u5ba2\u6237\u7aef\u5c06\u54cd\u5e94\u6b63\u6587\u4f5c\u4e3a\u9644\u4ef6\u8fdb\u884c\u4e0b\u8f7d\uff0c\u5e76\u6307\u5b9a\u4e0b\u8f7d\u6587\u4ef6\u540d\n                c.Header(\"Content-Disposition\", fmt.Sprintf(\"attachment; filename=%s\", f))\n                \/\/Content-Description:\u63cf\u8ff0\u4e86\u54cd\u5e94\u6b63\u6587\u7684\u6027\u8d28\uff0c\u901a\u5e38\u7528\u4e8e\u6307\u5b9a\u6587\u4ef6\u4e0b\u8f7d\n                c.Header(\"Content-Description\", \"File Transfer\")\n                \/\/Content-Type:\u6307\u793a\u54cd\u5e94\u6b63\u6587\u7684MIME\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u6307\u5b9a\u6587\u4ef6\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u6d41\n                c.Header(\"Content-Type\", \"application\/octet-stream\")\n                \/\/\u5c06\u672c\u5730\u6587\u4ef6\u4f5c\u4e3aHTTP\u54cd\u5e94\u7684\u6b63\u6587\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\n                c.File(filePath) \n\n        })\n        router.Run()\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u540e\u901a\u8fc7http:\/\/ip:8080\/getFile\/a.txt\u5373\u53ef\u4e0b\u8f7d\/tmp\u76ee\u5f55\u4e0b\u7684a.txt\u6587\u4ef6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u6279\u91cf\u4e0b\u8f7d\u6587\u4ef6\u540e\u6253\u5305\u4e3azip\u538b\u7f29\u5305\u5e76\u4e0b\u8f7d\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"archive\/zip\"\n\t\"io\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com\/gin-gonic\/gin\"\n)\n\nfunc main() {\n\trouter := gin.New()\n\t\/\/\u5b9a\u4e49\u52a8\u6001\u8def\u7531\n\trouter.GET(\"\/getFile\", func(c *gin.Context) {\n\t\tfileslice := &#91;]string{\"\/tmp\/linshi\/a.txt\", \"\/tmp\/linshi\/b.txt\", \"\/tmp\/linshi\/c.txt\"}\n\t\t\/\/Content-Disposition:\u6307\u793a\u5ba2\u6237\u7aef\u5982\u4f55\u5904\u7406\u54cd\u5e94\u6b63\u6587\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u544a\u8bc9\u5ba2\u6237\u7aef\u5c06\u54cd\u5e94\u6b63\u6587\u4f5c\u4e3a\u9644\u4ef6\u8fdb\u884c\u4e0b\u8f7d\uff0c\u5e76\u6307\u5b9a\u4e0b\u8f7d\u6587\u4ef6\u540d\n\t\tc.Header(\"Content-Disposition\", \"attachment; filename=download.zip\")\n\t\t\/\/Content-Description:\u63cf\u8ff0\u4e86\u54cd\u5e94\u6b63\u6587\u7684\u6027\u8d28\uff0c\u901a\u5e38\u7528\u4e8e\u6307\u5b9a\u6587\u4ef6\u4e0b\u8f7d\n\t\tc.Header(\"Content-Description\", \"File Transfer\")\n\t\t\/\/Content-Type:\u6307\u793a\u54cd\u5e94\u6b63\u6587\u7684MIME\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u6307\u5b9a\u6587\u4ef6\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u6d41\n\t\tc.Header(\"Content-Type\", \"application\/octet-stream\")\n\t\t\/\/\u521b\u5efazip\u6587\u4ef6\u5199\u5165\u5668,\u6ce8\u610f,\u662f\u9700\u8981\u5173\u95ed\u7684\n\t\tzipWriter := zip.NewWriter(c.Writer)\n\t\tdefer zipWriter.Close()\n\t\tfor _, v := range fileslice {\n\t\t\tfile, err := os.Open(v)\n\t\t\tif err != nil {\n\t\t\t\tlog.Fatal(err)\n\t\t\t}\n\t\t\tfileinfo, err := file.Stat()\n\t\t\tif err != nil {\n\t\t\t\tlog.Fatal(err)\n\t\t\t}\n\t\t\t\/\/\u63cf\u8ff0zip\u6587\u4ef6\u4e2d\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5143\u6570\u636e\u4fe1\u606f\n\t\t\theader := &amp;zip.FileHeader{\n\t\t\t\tName:   fileinfo.Name(),\n\t\t\t\tMethod: zip.Deflate, \/\/\u538b\u7f29\u65b9\u6cd5,0\u4e3a\u4e0d\u538b\u7f29,8\u4e3a\u4f7f\u7528DEFLATE\u7b97\u6cd5\u538b\u7f29,zip.Deflate\u5c31\u662f\u4f7f\u75288\u6765\u538b\u7f29\n\t\t\t}\n\t\t\t\/\/\u5411zip\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5177\u6709\u6307\u5b9a\u5143\u6570\u636e\u7684\u6587\u4ef6\u6216\u76ee\u5f55,\u8fd4\u56deio.Writer\u63a5\u53e3\n\t\t\twr, err := zipWriter.CreateHeader(header)\n\t\t\tif err != nil {\n\t\t\t\tlog.Fatal(err)\n\t\t\t}\n\t\t\t\/\/\u5c06file\u7684\u5185\u5bb9\u590d\u5236\u5230wr\u4e2d,\u6700\u540e\u5173\u95edfile\n\t\t\t_, err = io.Copy(wr, file)\n\t\t\tif err != nil {\n\t\t\t\tlog.Fatal(err)\n\t\t\t}\n\t\t\tdefer file.Close()\n\t\t}\n\t})\n\trouter.Run()\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6d4f\u89c8\u5668\u8bf7\u6c42http:\/\/ip:8080\/getFile\u5c06\u628a\u4e09\u4e2a\u6587\u4ef6\u6253\u8fdbdownload.zip\u4e2d\u5e76\u4e0b\u8f7d\u51fa\u6765<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\uff0cmultipart\u901a\u5e38\u6307\u7684\u662fmultipart\/form-data\uff0c\u8fd9\u662f\u4e00\u79cd\u7528\u4e8eHTTP\u534f\u8bae\u7684 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":11053,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-11052","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/11052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=11052"}],"version-history":[{"count":32,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/11052\/revisions"}],"predecessor-version":[{"id":17729,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/11052\/revisions\/17729"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/11053"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=11052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=11052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=11052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}