{"id":1722,"date":"2020-10-10T02:49:05","date_gmt":"2020-10-09T18:49:05","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1722"},"modified":"2022-02-16T21:51:44","modified_gmt":"2022-02-16T13:51:44","slug":"iris-file-upload-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/10\/iris-file-upload-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris \u7684 file upload"},"content":{"rendered":"<h1>file upload<\/h1>\n<p>\u5728\u4ecb\u7d39\u5b8c\u4f7f\u7528\u8005\u63d0\u4ea4\u8acb\u6c42\u7684\u8655\u7406\u65b9\u5f0f\u4e4b\u5f8c\uff0c\u63a5\u4e0b\u4f86\u8981\u4ecb\u7d39\u4e00\u500b\u5f88\u5e38\u6703\u61c9\u7528\u5230\u7684\u529f\u80fd\uff0c\u90a3\u5c31\u662f\u4e0a\u50b3\u6a94\u6848\uff0c\u63a5\u4e0b\u4f86\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u4f55\u8655\u7406\u4e0a\u50b3\u6a94\u6848\u7684\u8acb\u6c42\u3002<\/p>\n<p><!--more--><\/p>\n<h2>iris file upload<\/h2>\n<p>\u76f8\u4fe1\u5927\u5bb6\u61c9\u8a72\u5e38\u9047\u5230\u4e0a\u50b3\u7167\u7247\u6216\u5176\u4ed6\u6a94\u6848\u7684\u8acb\u6c42\uff0c\u4eca\u5929\u4f86\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5728iris\u88e1\u5982\u4f55\u8655\u7406\u6a94\u6848\u4e0a\u50b3\u7684\u8acb\u6c42\u3002<\/p>\n<h3>file upload<\/h3>\n<p>\u9996\u5148\u6211\u5011\u8981\u4ecb\u7d39\u8981\u5982\u4f55\u5728\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u4e0a\u4e0a\u50b3\u6a94\u6848\uff0c\u9019\u88e1\u5e38\u7528\u7684\u6280\u8853\u662f\u4f7f\u7528form POST\u7684file\u985e\u578b\uff0c\u6240\u4ee5\u6211\u5011\u8981\u5148\u6709\u4e00\u500b\u53ef\u4ee5\u4e0a\u50b3\u6a94\u6848\u7684view\uff0c\u4ee5\u53ca\u8655\u7406\u4e0a\u50b3\u6a94\u6848\u8acb\u6c42\u7684handler\uff0c\u6240\u4ee5\u63a5\u4e0b\u5229\u7528<a href=\"https:\/\/github.com\/iris-contrib\/examples\/tree\/master\/file-server\/upload-file\" title=\"iris example upload\" rel=\"nofollow noopener\" target=\"_blank\">iris example<\/a>\u4f86\u8ddf\u5927\u5bb6\u4ecb\u7d39iris\u4e0a\u7684\u8655\u7406\u3002<\/p>\n<h3>iris \u8655\u7406 file upload<\/h3>\n<p>\u9996\u5148\u8981\u8ddf\u5927\u5bb6\u4ecb\u7d39\u7684\u662fview\u8981\u600e\u9ebc\u8a2d\u8a08\u624d\u80fd\u4e0a\u50b3\u6a94\u6848\u5462\uff0c\u6240\u4ee5\u5927\u5bb6\u5148\u770b\u4e00\u4e0b\u4e0b\u5217\u7bc4\u4f8b<\/p>\n<pre><code class=\"language-html\">&lt;html&gt;\n\n&lt;head&gt;\n    &lt;title&gt;Upload file&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;form enctype=&quot;multipart\/form-data&quot; action=&quot;http:\/\/127.0.0.1:8080\/upload&quot; method=&quot;POST&quot;&gt;\n        &lt;input type=&quot;file&quot; name=&quot;uploadfile&quot; \/&gt;\n        &lt;input type=&quot;hidden&quot; name=&quot;token&quot; value=&quot;{{.}}&quot; \/&gt;\n        &lt;input type=&quot;submit&quot; value=&quot;upload&quot; \/&gt;\n    &lt;\/form&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u4e0a\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4e0a\u50b3\u6a94\u6848\u7684form\uff0c\u9078\u64c7\u6a94\u6848\u5f8c\u6309\u4e0bsubmit\u5c31\u53ef\u4ee5\u63d0\u4ea4\u4e0a\u50b3\u6a94\u6848\u7684\u8acb\u6c42\uff0c\u63a5\u4e0b\u4f86\u518d\u8ddf\u5927\u5bb6\u5206\u4eab\u5982\u4f55\u8655\u7406\u4e0a\u50b3\u6a94\u6848\u7684\u8acb\u6c42\u3002<\/p>\n<pre><code class=\"language-go\">    \/\/ Handle the post request from the upload_form.html to the server\n    app.Post(&quot;\/upload&quot;, iris.LimitRequestBodySize(maxSize+1&lt;&lt;20), func(ctx iris.Context) {\n        \/\/ Get the file from the request.\n        f, fh, err := ctx.FormFile(&quot;uploadfile&quot;)\n        if err != nil {\n            ctx.StatusCode(iris.StatusInternalServerError)\n            ctx.HTML(&quot;Error while uploading: &lt;b&gt;&quot; + err.Error() + &quot;&lt;\/b&gt;&quot;)\n            return\n        }\n        defer f.Close()\n\n        _, err = ctx.SaveFormFile(fh, filepath.Join(&quot;.\/uploads&quot;, fh.Filename))\n        if err != nil {\n            ctx.StatusCode(iris.StatusInternalServerError)\n            ctx.HTML(&quot;Error while uploading: &lt;b&gt;&quot; + err.Error() + &quot;&lt;\/b&gt;&quot;)\n            return\n        }\n    })\n\n    \/\/ start the server at http:\/\/localhost:8080 with post limit at 5 MB.\n    app.Listen(&quot;:8080&quot; \/* 0.*\/, iris.WithPostMaxMemory(maxSize))<\/code><\/pre>\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\u4ecb\u7d39\u5982\u4f55\u8655\u7406\u4e0a\u50b3\u6a94\u6848\u7684\u8acb\u6c42\uff0c\u6709\u5e7e\u9ede\u8981\u8aaa\u660e\u4e00\u4e0b\uff0c\u9996\u5148\u662f\u8655\u7406\u5c07\u8acb\u6c42\u6536\u4e0b\u4f86\u7684\u52d5\u4f5c\uff0c\u9019\u88e1\u4f7f\u7528<code>ctx.FormFile(fileName string)<\/code>\u5c07\u4e0a\u50b3\u7684\u6a94\u6848\u5132\u5b58\u5230\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u63a5\u4e0b\u4f86\u900f\u904e<br \/>\n<code>ctx.SaveFormFile(fh, filepath.Join(&quot;.\/uploads&quot;, fh.Filename))<\/code>\u9019\u65b9\u6cd5\u5c07\u8a18\u61b6\u9ad4\u4e2d\u7684\u6a94\u6848\u5132\u5b58\u6210\u5be6\u9ad4\u6a94\u6848\uff0c\u9019\u6a23\u8655\u7406\u4e0a\u50b3\u6a94\u6848\u7684\u8acb\u6c42\u5c31\u7d50\u675f\u4e86\uff0c\u4e0d\u904e\u9084\u6709\u4e00\u9ede\u8981\u6ce8\u610f\u4e00\u4e0b\uff0c\u90a3\u5c31\u662f\u8a18\u5f97\u8981\u8a2d\u5b9a\u4e0a\u50b3\u6a94\u6848\u7684\u6700\u5927\u503c\uff0c\u4e0d\u7136\u9047\u5230\u60e1\u610f\u7684\u8acb\u6c42\u6703\u628a\u7db2\u9801\u4f3a\u670d\u5668\u7684\u786c\u789f\u7a7a\u9593\u585e\u7206\uff0c\u6240\u4ee5\u9019\u908a\u4f7f\u7528<code>app.Listen(&quot;:8080&quot; \/* 0.*\/, iris.WithPostMaxMemory(maxSize))<\/code>\u4f86\u8a2d\u5b9a\u6a94\u6848\u6700\u5927\u503c\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u4eca\u5929\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u4f55\u8655\u7406\u9019\u500bform POST\u7684\u4e00\u500b\u7279\u6b8a\u61c9\u7528file upload\uff0c\u6240\u4ee5iris\u8655\u7406\u4f7f\u7528\u8005\u7684\u8acb\u6c42\u8b1b\u5230\u9019\u88e1\u544a\u4e00\u6bb5\u843d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>file upload \u5728\u4ecb\u7d39\u5b8c\u4f7f\u7528\u8005\u63d0\u4ea4\u8acb\u6c42\u7684\u8655\u7406\u65b9\u5f0f\u4e4b\u5f8c\uff0c\u63a5\u4e0b\u4f86\u8981\u4ecb\u7d39\u4e00\u500b\u5f88\u5e38\u6703\u61c9\u7528\u5230\u7684\u529f\u80fd\uff0c\u90a3\u5c31\u662f\u4e0a\u50b3\u6a94 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/10\/iris-file-upload-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris \u7684 file upload\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84],"class_list":["post-1722","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1722"}],"version-history":[{"count":12,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1722\/revisions"}],"predecessor-version":[{"id":2954,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1722\/revisions\/2954"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}