{"id":695,"date":"2024-12-02T22:14:53","date_gmt":"2024-12-02T14:14:53","guid":{"rendered":"http:\/\/www.chan.ink\/?p=695"},"modified":"2024-12-02T22:14:56","modified_gmt":"2024-12-02T14:14:56","slug":"change%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/www.chan.ink\/index.php\/2024\/12\/02\/change%e4%ba%8b%e4%bb%b6\/","title":{"rendered":"change\u4e8b\u4ef6"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>{% load static %}\n\n&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    &lt;title>Document&lt;\/title>\n&lt;\/head>\n\n&lt;body>\n    &lt;h1>\u793a\u4f8b1\uff1a\u76f4\u63a5\u901a\u8fc7\u79d8\u94a5\u8fdb\u884c\u5220\u9664\u4e0a\u4f20\u6587\u4ef6&lt;\/h1>\n    &lt;input type=\"file\" name=\"upload_file\" id=\"uploadFile\" multiple \/>\n\n    &lt;script src=\"{% static 'js\/jquery-3.4.1.min.js' %} \"> &lt;\/script>\n    &lt;script src=\"{% static 'js\/cos-js-sdk-v5.min.js' %} \"> &lt;\/script>\n    &lt;script>\n        var cos;\n        $(function () {\n            initCOS();\n            bindChangeFileInput();\n        });\n\n        function initCOS() {\n            cos = new COS({\n                SecretId: 'AKIDFPJSXQEk8PXVL3Tx5zf6MSL0Sf7Qoikg',\n                SecretKey: 'yiCWfZCXcQxJZlqncKvRu5DKHySg8sMp',\n            });\n        }\n\n        function bindChangeFileInput() {\n            $(\"#uploadFile\").change(function () {\n                \/\/ \u83b7\u53d6\u8981\u4e0a\u4f20\u7684\u6240\u6709\u6587\u4ef6\u5bf9\u8c61\u5217\u8868\n                var files = $(this)&#91;0].files;\n                $.each(files, function (index, fileObject) {\n                    var fileName = fileObject.name;\n                    \/\/ \u4e0a\u4f20\u6587\u4ef6\n                    cos.putObject({\n                        Bucket: 'wangyang-1251317460', \/* \u5fc5\u987b *\/\n                        Region: 'ap-chengdu',     \/* \u5b58\u50a8\u6876\u6240\u5728\u5730\u57df\uff0c\u5fc5\u987b\u5b57\u6bb5 *\/\n                        Key: fileName,              \/* \u5fc5\u987b *\/\n                        StorageClass: 'STANDARD',\n                        Body: fileObject, \/\/ \u4e0a\u4f20\u6587\u4ef6\u5bf9\u8c61\n                        onProgress: function (progressData) {\n                            console.log(\"\u6587\u4ef6\u4e0a\u4f20\u8fdb\u5ea6--->\",fileName,JSON.stringify(progressData));\n                        }\n                    }, function (err, data) {\n                        console.log(err || data);\n                    });\n\n                })\n\n            })\n        }\n    &lt;\/script>\n&lt;\/body>\n\n&lt;\/html><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u662f\u600e\u4e48\u7ed1\u5b9a\u7684\u4e8b\u60c5\uff0c\u6709\u70b9\u6655\u3002\u6211\u8bb0\u5f97\u4ee5\u524d\u7684\u7ed1\u5b9a\uff0c\u662f\u6709\u4e2aonclick\u597d\u50cf\u3002<br>$(&#8220;#uploadFile&#8221;).change(function () \u8fd9\u662f\u600e\u4e48\u7ed1\u5b9a\u7684\u3002&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>$(\"#uploadFile\").change(function () {...})<\/code> \u662f\u901a\u8fc7 <strong>jQuery<\/strong> \u6765\u7ed1\u5b9a\u4e8b\u4ef6\u7684\u65b9\u5f0f\uff0c\u5177\u4f53\u6765\u8bf4\uff0c\u5b83\u662f\u7ed1\u5b9a\u4e86\u4e00\u4e2a <code>change<\/code> \u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u5230\u4e00\u4e2a\u6587\u4ef6\u8f93\u5165\u6846\uff08<code>&lt;input type=\"file\"&gt;<\/code>\uff09\u4e0a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8be6\u7ec6\u5206\u6790\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>jQuery\u7684\u9009\u62e9\u5668<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>$(\"#uploadFile\")<\/code> \u8fd9\u4e2a\u90e8\u5206\u662f\u4e00\u4e2a <strong>jQuery \u9009\u62e9\u5668<\/strong>\uff0c\u7528\u4e8e\u9009\u53d6\u9875\u9762\u4e0a <code>id=\"uploadFile\"<\/code> \u7684\u5143\u7d20\uff0c\u5373\u6587\u4ef6\u8f93\u5165\u6846 <code>&lt;input type=\"file\" id=\"uploadFile\" \/><\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>change<\/code>\u4e8b\u4ef6<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>change<\/code> \u662f\u4e00\u79cd <strong>HTML\u4e8b\u4ef6<\/strong>\uff0c\u901a\u5e38\u7528\u4e8e\u8868\u5355\u63a7\u4ef6\uff0c\u5f53\u63a7\u4ef6\u7684\u503c\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\u3002\u5bf9\u4e8e\u6587\u4ef6\u8f93\u5165\u6846\uff08<code>&lt;input type=\"file\"><\/code>\uff09\uff0c\u5f53\u7528\u6237\u9009\u62e9\u4e86\u6587\u4ef6\u540e\uff0c<code>change<\/code>\u4e8b\u4ef6\u4f1a\u88ab\u89e6\u53d1\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff0c\u7528\u6237\u70b9\u51fb\u4e86\u6587\u4ef6\u8f93\u5165\u6846\uff0c\u9009\u62e9\u4e86\u6587\u4ef6\u540e\uff0c\u8fd9\u65f6 <code>change<\/code> \u4e8b\u4ef6\u5c31\u4f1a\u88ab\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u7ed1\u5b9a<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>$(\"#uploadFile\").change(function () {...})<\/code> \u8fd9\u4e00\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u662f <strong>\u7ed1\u5b9a<\/strong> \u4e00\u4e2a <strong>\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<\/strong>\uff08\u5373 <code>function () {...}<\/code>\uff09\u5230 <code>#uploadFile<\/code> \u8fd9\u4e2a\u6587\u4ef6\u8f93\u5165\u6846\u7684 <code>change<\/code> \u4e8b\u4ef6\u4e0a\u3002<\/li>\n\n\n\n<li>\u5f53\u7528\u6237\u9009\u62e9\u6587\u4ef6\u540e\uff0c<code>change<\/code> \u4e8b\u4ef6\u4f1a\u88ab\u89e6\u53d1\uff0c\u4ece\u800c\u8c03\u7528\u7ed1\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u56de\u8c03\u51fd\u6570<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>function () {...}<\/code> \u662f\u4e8b\u4ef6\u89e6\u53d1\u540e\u6267\u884c\u7684\u51fd\u6570\u3002\u5f53\u6587\u4ef6\u8f93\u5165\u6846\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u56de\u8c03\u51fd\u6570\u4f1a\u63a5\u6536\u5230\u5f53\u524d\u9009\u4e2d\u7684\u6587\u4ef6\u3002<\/li>\n\n\n\n<li>\u4ee3\u7801\u4e2d\u7684 <code>var files = $(this)[0].files;<\/code> \u83b7\u53d6\u5230\u7528\u6237\u9009\u62e9\u7684\u6587\u4ef6\u5217\u8868\uff0c\u63a5\u7740\u901a\u8fc7 <code>$.each<\/code> \u904d\u5386\u6bcf\u4e2a\u6587\u4ef6\u5e76\u4e0a\u4f20\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u4e0d\u9700\u8981 <code>onclick<\/code>\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>onclick<\/code> \u4e8b\u4ef6\u901a\u5e38\u662f\u7528\u4e8e\u5904\u7406\u9f20\u6807\u70b9\u51fb\u4e8b\u4ef6\u7684\uff0c\u4f8b\u5982\u6309\u94ae\u7684\u70b9\u51fb\u3002\u5bf9\u4e8e\u6587\u4ef6\u4e0a\u4f20\u7684\u573a\u666f\uff0c<code>&lt;input type=\"file\"&gt;<\/code> \u6807\u7b7e\u672c\u8eab\u5e76\u6ca1\u6709 <code>onclick<\/code> \u4e8b\u4ef6\u6765\u5904\u7406\u6587\u4ef6\u9009\u62e9\u3002\u76f8\u53cd\uff0c<code>&lt;input type=\"file\"&gt;<\/code> \u5143\u7d20\u4f1a\u89e6\u53d1 <code>change<\/code> \u4e8b\u4ef6\uff0c\u5f53\u7528\u6237\u9009\u62e9\u6587\u4ef6\u540e\uff0c\u89e6\u53d1\u7684\u5c31\u662f\u8fd9\u4e2a\u4e8b\u4ef6\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528 <code>change<\/code> \u4e8b\u4ef6\u6765\u5904\u7406\u6587\u4ef6\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u89e3\u91ca\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">javascript\u590d\u5236\u4ee3\u7801<code>$(\"#uploadFile\").change(function () {\n    \/\/ \u83b7\u53d6\u7528\u6237\u9009\u62e9\u7684\u6587\u4ef6\u5217\u8868\n    var files = $(this)[0].files;\n    $.each(files, function (index, fileObject) {\n        var fileName = fileObject.name;  \/\/ \u83b7\u53d6\u6587\u4ef6\u540d\n        \/\/ \u4e0a\u4f20\u6587\u4ef6\u5230 COS\uff08\u817e\u8baf\u4e91\u5bf9\u8c61\u5b58\u50a8\uff09\n        cos.putObject({\n            Bucket: 'wangyang-1251317460', \/\/ \u5b58\u50a8\u6876\n            Region: 'ap-chengdu',           \/\/ \u533a\u57df\n            Key: fileName,                  \/\/ \u6587\u4ef6\u540d\n            StorageClass: 'STANDARD',       \/\/ \u5b58\u50a8\u7c7b\u578b\n            Body: fileObject,               \/\/ \u4e0a\u4f20\u7684\u6587\u4ef6\u5bf9\u8c61\n            onProgress: function (progressData) {  \/\/ \u4e0a\u4f20\u8fdb\u5ea6\u56de\u8c03\n                console.log(\"\u6587\u4ef6\u4e0a\u4f20\u8fdb\u5ea6---&gt;\", fileName, JSON.stringify(progressData));\n            }\n        }, function (err, data) {\n            console.log(err || data);  \/\/ \u4e0a\u4f20\u7ed3\u679c\n        });\n    });\n});\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u4ef6\u7ed1\u5b9a<\/strong>\uff1a<code>$(\"#uploadFile\").change(function () {...})<\/code> \u4f7f\u7528\u4e86 <strong>jQuery<\/strong> \u6765\u4e3a <code>#uploadFile<\/code>\uff08\u6587\u4ef6\u8f93\u5165\u6846\uff09\u7ed1\u5b9a\u4e86 <code>change<\/code> \u4e8b\u4ef6\u3002\u5f53\u6587\u4ef6\u9009\u62e9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u56de\u8c03\u51fd\u6570\u4f1a\u88ab\u89e6\u53d1\u3002<\/li>\n\n\n\n<li><strong>\u56de\u8c03\u51fd\u6570<\/strong>\uff1a\u56de\u8c03\u51fd\u6570\u5904\u7406\u7684\u662f\u6587\u4ef6\u9009\u62e9\u540e\u7684\u903b\u8f91\uff0c\u5305\u62ec\u83b7\u53d6\u6587\u4ef6\u5217\u8868\u5e76\u4e0a\u4f20\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u4e0e<code>onclick<\/code>\u7684\u533a\u522b<\/strong>\uff1a<code>onclick<\/code> \u662f\u9f20\u6807\u70b9\u51fb\u4e8b\u4ef6\uff0c\u800c <code>change<\/code> \u4e8b\u4ef6\u662f\u6587\u4ef6\u8f93\u5165\u6846\u7279\u6709\u7684\u4e8b\u4ef6\uff0c\u5728\u6587\u4ef6\u9009\u62e9\u53d1\u751f\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u4e0d\u9700\u8981\u4f7f\u7528 <code>onclick<\/code> \u5904\u7406\u6587\u4ef6\u9009\u62e9\uff0c\u76f4\u63a5\u4f7f\u7528 <code>change<\/code> \u4e8b\u4ef6\u66f4\u7b26\u5408\u6587\u4ef6\u4e0a\u4f20\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u662f\u600e\u4e48\u7ed1\u5b9a\u7684\u4e8b\u60c5\uff0c\u6709\u70b9\u6655\u3002\u6211\u8bb0\u5f97\u4ee5\u524d\u7684\u7ed1\u5b9a\uff0c\u662f\u6709\u4e2aonclick\u597d\u50cf\u3002$(&#8220;#uploadFi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-695","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/comments?post=695"}],"version-history":[{"count":1,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/695\/revisions"}],"predecessor-version":[{"id":697,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/695\/revisions\/697"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}