{"id":329,"date":"2013-07-31T06:59:30","date_gmt":"2013-07-30T21:59:30","guid":{"rendered":"http:\/\/morishige.jp\/blog\/?p=329"},"modified":"2013-07-31T07:06:25","modified_gmt":"2013-07-30T22:06:25","slug":"actionscritp3-multipartform-data%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%81%ae%e3%82%a2%e3%83%83%e3%83%97%e3%83%ad%e3%83%bc%e3%83%89","status":"publish","type":"post","link":"https:\/\/morishige.jp\/blog\/archives\/329","title":{"rendered":"ActionScritp3 multipart\/form-data\u3067\u30c7\u30fc\u30bf\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9"},"content":{"rendered":"<p>\u30e2\u30d0\u30a4\u30ebAIR\u3067\u30ab\u30e1\u30e9\u64ae\u5f71\u3057\u305f\u5199\u771f\u3092multipart\/form-data\u3067Web\u306b\u30a2\u30c3\u30d7\u3059\u308b\u6d41\u308c\u306e\u30e1\u30e2\u3002<br \/>\n\u5199\u771f\u304cbitmapData\u3068\u3044\u3046\u6bb5\u968e\u304b\u3089\u30b9\u30bf\u30fc\u30c8\u3002<\/p>\n<pre>\r\n\/\/ Jpeg \u5909\u63db\r\nvar byteArray:ByteArray = new ByteArray();\r\nbitmapData.encode(bitmapData.rect, new flash.display.JPEGEncoderOptions(), byteArray);\r\n\r\n\/\/ Base64 \u5909\u63db\r\nvar base64Encoder:Base64Encoder = new mx.utils.Base64Encoder();\r\nbase64Encoder.encodeBytes(byteArray);\r\n\r\n\/\/ POST\u30c7\u30fc\u30bf\u4f5c\u6210\r\nvar formBuilder:MultipartFormDataBuilder = new jp.psyark.net.MultipartFormDataBuilder(\"207542376\u9069\u5f53\");\r\nformBuilder.addPart(\"apiKey\", \"123456\u30db\u30b2\");\r\nformBuilder.addPart(\"inputFile\", byteArray, \"dummyName.jpg\", true);\r\n\r\nvar urlRequest:URLRequest = new URLRequest(url);\r\nformBuilder.configure(urlRequest);\r\n\r\nvar urlLoader:URLLoader = new URLLoader();\r\n\/\/ \u3053\u3053\u3089\u3067\u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc\u306e\u8ffd\u52a0\u51e6\u7406\u3002\r\nurlLoader.load(urlRequest);\r\n<\/pre>\n<p>\u554f\u984c\u306fPOST\u30c7\u30fc\u30bf\u306e\u4f5c\u6210\u306e\u3068\u3053\u308d\u3002<br \/>\n<a href=\"http:\/\/www.libspark.org\/wiki\/psyark\/MultipartFormDataBuilder\">http:\/\/www.libspark.org\/wiki\/psyark\/MultipartFormDataBuilder<\/a><br \/>\n\u306b\u3042\u308bMultipartFormDataBuilder\u3092\u5229\u7528\u3057\u307e\u3057\u305f\u304c\u3001\u3046\u307e\u304f\u3044\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<br \/>\n\u305d\u3093\u306a\u3068\u304d\u3001<a href=\"http:\/\/www.kuma-de.com\/blog\/2010-10-07\/2352\">[AS3] PHP\u3068AS3\u306e\u9023\u643a\u3000multipart\/form-data\u3067\u30c7\u30fc\u30bf\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9 http:\/\/www.kuma-de.com\/blog\/2010-10-07\/2352<\/a> \u304c\u5927\u5909\u53c2\u8003\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u306e\u30b5\u30a4\u30c8\u306eMultipartFormDataBuilder.as\u3067\u3082\u307e\u3060\u52d5\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u7d50\u8ad6\u3068\u3057\u3066\u306f\u3001\u8ffd\u8a18\u306b\u3042\u308b\u6700\u5f8c\u306e\u30d0\u30a6\u30f3\u30c0\u30ea\u306e\u554f\u984c\u304c\u539f\u56e0\u3067\u3057\u305f\u3002\u305d\u3053\u3067\u3001addPart\u306e\u7b2c4\u5f15\u6570 last \u3092\u8ffd\u52a0\u3057\u3066\u3001\u30d0\u30a6\u30f3\u30c0\u30ea\u306e\u6700\u5f8c\u306b\u300c&#8211;\u300d\u304c\u8ffd\u52a0\u3055\u308c\u308b\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3057\u305f\u3002\u53d7\u3051\u53d6\u308a\u5074\u3092php\u3067\u5b9f\u88c5\u3057\u305f\u5834\u5408\u306f\u3001\u5fc5\u8981\u306a\u304b\u3063\u305f\u3067\u3059\u3002java\u306etomcat\u3060\u3068\u6700\u5f8c\u306e\u300c&#8211;\u300d\u304c\u5fc5\u8981\u3067\u3057\u305f\u3002<\/p>\n<p>\u7406\u60f3\u7684\u306a\u4fee\u6b63\u306f\u3001configure()\u5b9f\u884c\u6642\u306b\u300c&#8211;\u300d\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304b\u306a\u3002\u6700\u5f8c\u304b\u3069\u3046\u304b\u306f\u30b3\u30fc\u30c9\u5074\u3067\u52dd\u624b\u306b\u5224\u65ad\u3057\u3066\u304f\u308c\u308c\u3070\u5341\u5206\u3067\u3059\u306d\u3002\u3042\u3068\u3001Content-Type:\u3082\u30d9\u30bf\u66f8\u304d\u3082\u76f4\u3055\u306a\u3044\u3068\u601d\u3044\u3064\u3064\u305d\u306e\u307e\u307ew<\/p>\n<pre>\r\npackage jp.psyark.net {\r\n\timport flash.net.URLRequest;\r\n\timport flash.net.URLRequestMethod;\r\n\timport flash.utils.ByteArray;\r\n\t\r\n\t\/**\r\n\t * multipart\/form-data\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u7c21\u5358\u306a\u30af\u30e9\u30b9\u3067\u3059\u3002\r\n\t *\/\r\n\tpublic class MultipartFormDataBuilder {\r\n\t\tprotected var _boundary:String;\r\n\t\tprotected var byteArray:ByteArray;\r\n\t\t\r\n\t\t\/**\r\n\t\t * \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u3059\u3002\r\n\t\t * \r\n\t\t * @param boundary \u30d0\u30a6\u30f3\u30c0\u30ea\uff08\u5883\u754c\u7dda\uff09\u6587\u5b57\u5217\u3067\u3059\u3002\u9001\u4fe1\u3059\u308b\u4ed6\u306e\u30c7\u30fc\u30bf\u4e2d\u306b\u51fa\u73fe\u3057\u306a\u3044\u6587\u5b57\u5217\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\r\n\t\t *\/\r\n\t\tpublic function MultipartFormDataBuilder(boundary:String) {\r\n\t\t\t_boundary = boundary;\r\n\t\t\tbyteArray = new ByteArray();\r\n\t\t\taddBoundary();\r\n\t\t}\r\n\t\t\r\n\t\t\/**\r\n\t\t * \u30d1\u30fc\u30c8\uff08\u90e8\u5206\uff09\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\r\n\t\t * \r\n\t\t * @param name \u3053\u306e\u30d1\u30fc\u30c8\u306e\u540d\u524d\u3067\u3059\u3002content-disposition\u30d8\u30c3\u30c0\u306ename\u5c5e\u6027\u306b\u4f7f\u308f\u308c\u307e\u3059\u3002\r\n\t\t * @param data \u3053\u306e\u30d1\u30fc\u30c8\u306e\u30c7\u30fc\u30bf\u3067\u3059\u3002ByteArray\u4ee5\u5916\u306e\u5024\u306f\u6587\u5b57\u5217\u3068\u3057\u3066\u8a55\u4fa1\u3055\u308c\u307e\u3059\u3002\r\n\t\t * @param filename \u3053\u306e\u30d1\u30fc\u30c8\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u3059\u3002null\u4ee5\u5916\u306e\u5024\u3092\u6e21\u3057\u305f\u5834\u5408\u3001content-disposition\u30d8\u30c3\u30c0\u306efilename\u5c5e\u6027\u306b\u4f7f\u308f\u308c\u307e\u3059\u3002\r\n\t\t * @param last \u6700\u5f8c\u306e\u30d1\u30fc\u30c8\u306e\u6642\u3060\u3051true\u306b\u3059\u308b\u3002\u30d0\u30a6\u30f3\u30c0\u30ea\u306b\u6700\u5f8c\u3092\u610f\u5473\u3059\u308b--\u3092\u8ffd\u52a0\u3059\u308b\u3002\r\n\t\t *\/\r\n\t\tpublic function addPart(name:String, data:*, filename:String=null, last:Boolean = false):void {\r\n\t\t\tbyteArray.writeUTFBytes('Content-disposition: form-data; name=\"' + name + '\"');\r\n\t\t\tif (filename != null) {\r\n\t\t\t\tbyteArray.writeUTFBytes('; filename=\"' + filename + '\"');\r\n\t\t\t\tbyteArray.writeUTFBytes(\"\\r\\n\");\r\n\t\t\t\tbyteArray.writeUTFBytes(\"Content-Type: image\/jpeg\"); \/\/JPEG\u7528\r\n\t\t\t}\r\n\t\t\tbyteArray.writeUTFBytes(\"\\r\\n\\r\\n\");\r\n\t\t\tif (data is ByteArray) {\r\n\t\t\t\tbyteArray.writeBytes(data);\r\n\t\t\t} else {\r\n\t\t\t\tbyteArray.writeUTFBytes(data);\r\n\t\t\t}\r\n\t\t\tbyteArray.writeUTFBytes(\"\\r\\n\");\r\n\t\t\taddBoundary(last);\r\n\t\t}\r\n\t\t\r\n\t\t\/**\r\n\t\t * multipart\/form-data\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3068\u3057\u3066\u4f7f\u3048\u308b\u3088\u3046\u306b\u3001URLRequest\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002\r\n\t\t *\/\r\n\t\tpublic function configure(request:URLRequest):void {\r\n\t\t\trequest.method = URLRequestMethod.POST;\r\n\t\t\trequest.data = byteArray;\r\n\t\t\trequest.contentType = \"multipart\/form-data; boundary=\" + _boundary;\r\n\t\t}\r\n\t\t\r\n\t\tprivate function addBoundary(last:Boolean = false):void {\r\n\t\t\tif (last) {\r\n\t\t\t\tbyteArray.writeUTFBytes(\"--\" + _boundary + \"--\\r\\n\");\r\n\t\t\t} else {\r\n\t\t\t\tbyteArray.writeUTFBytes(\"--\" + _boundary + \"\\r\\n\");\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30e2\u30d0\u30a4\u30ebAIR\u3067\u30ab\u30e1\u30e9\u64ae\u5f71\u3057\u305f\u5199\u771f\u3092multipart\/form-data\u3067Web\u306b\u30a2\u30c3\u30d7\u3059\u308b\u6d41\u308c\u306e\u30e1\u30e2\u3002 \u5199\u771f\u304cbitmapData\u3068\u3044\u3046\u6bb5\u968e\u304b\u3089\u30b9\u30bf\u30fc\u30c8\u3002 \/\/ Jpeg \u5909\u63db var byteArray:ByteA [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-actionscript"],"_links":{"self":[{"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":6,"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/morishige.jp\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}