{"id":1633,"date":"2017-04-24T23:50:06","date_gmt":"2017-04-24T14:50:06","guid":{"rendered":"http:\/\/lovelinux.mydns.jp\/?p=1633"},"modified":"2017-04-24T23:50:06","modified_gmt":"2017-04-24T14:50:06","slug":"react%e3%81%a7-react-dropzone-%e3%81%a8-axios%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%a2%e3%83%83%e3%83%97%e3%83%ad%e3%83%bc%e3%83%89%e3%82%92%e5%ae%9f%e8%a3%85","status":"publish","type":"post","link":"https:\/\/lovelinux.mydns.jp\/?p=1633","title":{"rendered":"React\u3067 react-dropzone \u3068 axios\u3092\u4f7f\u3063\u3066\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u308b\uff08\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u306fRails\uff09"},"content":{"rendered":"<h3>\u306f\u3058\u3081\u306b<\/h3>\n<p>React\u3067\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u304f\u3066\u3001\u3069\u306e\u3088\u3046\u306a\u65b9\u6cd5\u304c\u3042\u308b\u306e\u304b\u8abf\u3079\u3066\u307f\u305f\u3002<br \/>\n\u3042\u307e\u308a\u6df1\u304f\u8abf\u3079\u3066\u306f\u306a\u3044\u304c\u3001\uff11\u3064\u3081\u306b\u826f\u3055\u305d\u3046\u3060\u3068\u611f\u3058\u305f react-dropzone \u3092\u4f7f\u3063\u3066\u307f\u305f\u3089\u3001<br \/>\n\u3053\u308c\u304c\u304b\u306a\u308a\u3044\u3044\uff01\uff01<\/p>\n<p>\u3042\u307e\u308a\u306b\u6c17\u306b\u5165\u3063\u3066\u3057\u307e\u3063\u305f\u306e\u3067\u3001react-dropzone\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5bfe\u8c61\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u9078\u3093\u3067\u3001<br \/>\naxios\u3067POST\u3057\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306eRails\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u3056\u3063\u3068\u7d39\u4ecb\u3057\u307e\u3059\u3002<br \/>\nReact\u306e\u3053\u3068\u306f\u5206\u304b\u3063\u3066\u3044\u308b\u524d\u63d0\u3067\u3059\u3059\u3081\u307e\u3059\u306e\u3067\u3001\u3042\u3057\u304b\u3089\u305a\u3002<\/p>\n<p>\u51fa\u6765\u4e0a\u304c\u308a\u306e\u30a4\u30e1\u30fc\u30b8\u306f\u3053\u3093\u306a\u611f\u3058<br \/>\n\u753b\u9762\u5de6\u4e0a\u306b \u30d5\u30a1\u30a4\u30eb\u3092\u30c9\u30ed\u30c3\u30d7\u3059\u308b\u5834\u6240\u304c\u3042\u308a\u3001\u305d\u306e\u53f3\u5074\u306b\u30c9\u30ed\u30c3\u30d7\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b\u5834\u6240\u304c\u3042\u308b\u3002<br \/>\n\u3067\u3001\u753b\u9762\u4e0b\u90e8\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5bfe\u8c61\u30d5\u30a1\u30a4\u30eb\u306e\u30d7\u30ec\u30d3\u30e5\u30fc\u8868\u793a\u3002<\/p>\n<p><a href=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/dropzone-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/lovelinux.mydns.jp\/wp-content\/uploads\/2017\/04\/dropzone-2.png\" alt=\"dropzone-2\" width=\"932\" height=\"674\" class=\"alignnone size-full wp-image-1635\" \/><\/a><\/p>\n<h3>npm \u3067\u5fc5\u8981\u306a\u3082\u306e\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>[bash]<br \/>\n$ npm install &#8211;save react-dropzone axios<\/p>\n<p>[\/bash]<\/p>\n<h3>\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u753b\u9762\u5b9f\u88c5<\/h3>\n<p>\u4e00\u6c17\u306b\u8cbc\u308a\u4ed8\u3051\u3061\u3083\u3044\u307e\u3057\u305f\u306e\u3067\u3001\u3056\u3063\u3068\u8aac\u660e\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br \/>\nDropZone \u30bf\u30b0\u3067\u3001\u30c9\u30e9\u30c3\u30b0\u30c9\u30ed\u30c3\u30d7\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3067\u304d\u308b\u6a5f\u80fd\u304c<br \/>\n\u3067\u304d\u3061\u3083\u3044\u307e\u3059\u3002accept\u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u53d7\u3051\u4ed8\u3051\u308b\u753b\u50cf\u306e\u7a2e\u985e\uff08\u30d5\u30a1\u30a4\u30eb\u306e\u7a2e\u985e\uff09\u3092\u6307\u5b9a\u3057\u3001<br \/>\nonDrop\u3067\u30c9\u30e9\u30c3\u30b0\u30c9\u30ed\u30c3\u30d7\u3055\u308c\u305f\u6642\u306e\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306fonDrop\u3067\u53d7\u4ed8\u53ef\u80fd\u306a\u30d5\u30a1\u30a4\u30eb\u3001\u4e0d\u53ef\u80fd\u306a\u30d5\u30a1\u30a4\u30eb\u3092\u5f15\u6570\u3067\u53d7\u3051\u53d6\u308a\u3001<br \/>\n\u30b9\u30c6\u30fc\u30c8\u3092\u5909\u3048\u3066\u3001\u30c9\u30ed\u30c3\u30d7\u9818\u57df\u306e\u53f3\u5074\u306b\u305d\u308c\u3089\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u5217\u6319\u3057\u307e\u3059\u3002<br \/>\n\u3068\u540c\u6642\u306b\u3001\u753b\u9762\u4e0b\u90e8\u306b\u53d7\u4ed8\u53ef\u80fd\u306a\u30d5\u30a1\u30a4\u30eb\u306e\u30d7\u30ec\u30d3\u30e5\u30fc\u3092\u8868\u793a\u3057\u3061\u3083\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u304c\u3044\u3068\u3082\u7c21\u5358\u306a\u3093\u3067\u3059\u3002\u3073\u3063\u304f\u308a\u3067\u3059\u3088\u3002<\/p>\n<p>\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u30dc\u30bf\u30f3\u304c\u30af\u30ea\u30c3\u30af\u3055\u308c\u305f\u3089\u3001FormData \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u308a\u3001<br \/>\n\u305d\u3053\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5bfe\u8c61\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8a70\u3081\u8fbc\u3093\u3067\u3001axios\u3067POST\u3057\u307e\u3059\u3002<br \/>\n\u305f\u3063\u305f\u3053\u308c\u3060\u3051\u306a\u3093\u3067\u3059\u3088\u3001\u30db\u30f3\u30c8\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n<p>\u4f55\u500b\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u308b\u304b\u306f\u4e0d\u5b9a\u306a\u306e\u3067\u3001<br \/>\nfile_1\u3001file_2\u3001file_3\u306e\u3088\u3046\u306a\u9023\u756a\u306e\u540d\u524d\u3092\u3064\u3051\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>[bash]<br \/>\nimport React from &#8220;react&#8221;<br \/>\nimport DropZone from &#8220;react-dropzone&#8221;<br \/>\nimport axios from &#8220;axios&#8221;<br \/>\nimport {HOST_NAME} from &#8220;..\/constants\/index&#8221;<\/p>\n<p>class Upload extends React.Component {<br \/>\n  constructor(props){<br \/>\n    super(props)<br \/>\n    this.state = {acceptedfiles: [],rejectedfiles: []}<br \/>\n  }<\/p>\n<p>  handleDrop(accepted, rejected) {<br \/>\n    this.setState({acceptedfiles: accepted, rejectedfiles: rejected})<br \/>\n  }<\/p>\n<p>  handleOpenClick() {<br \/>\n    this.dropzone.open()<br \/>\n  }<\/p>\n<p>  handleUpload() {<\/p>\n<p>    let data = new FormData()<br \/>\n    data.append(&#8216;name&#8217;, &#8216;Tom&#8217;)<br \/>\n    data.append(&#8216;age&#8217;, &#8217;28&#8217;)<\/p>\n<p>    let cnt = 1<br \/>\n    this.state.acceptedfiles.map(file =>{<br \/>\n      data.append(&#8216;file_&#8217; + cnt, file)<br \/>\n      cnt+=1<br \/>\n    })<\/p>\n<p>    axios.post(HOST_NAME + &#8216;\/upload_image&#8217;, data)<\/p>\n<p>  }<\/p>\n<p>  render(){<br \/>\n    let prev = []<br \/>\n    this.state.acceptedfiles.map(f =><br \/>\n      prev.push(<\/p>\n<div className=\"preview\">\n          <img key={f.name} src={f.preview} width=\"100\" height=\"100\" \/>\n        <\/div>\n<p>      )<br \/>\n    )<\/p>\n<p>    return (<\/p>\n<section>\n<div className=\"uploadFile\">\n          Hello Upload&#8230;<br \/>\n          <DropZone\n            ref={(node) => this.dropzone = node}<br \/>\n            accept=&#8221;image\/jpg,image\/png&#8221;<br \/>\n            onDrop={(accepted, rejected)=> this.handleDrop(accepted, rejected)}<br \/>\n          ><br \/>\n            \u3053\u3053\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u30c9\u30ed\u30c3\u30d7\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n          <\/DropZone><br \/>\n          <button type=\"button\" onClick={(e) => this.handleOpenClick(e)}><br \/>\n              Open Dropzone<br \/>\n          <\/button><br \/>\n          <button type=\"button\" onClick={(e) => this.handleUpload(e)}><br \/>\n              Upload\u5b9f\u884c<br \/>\n          <\/button>\n        <\/div>\n<div className=\"uploadFile\">\n<h2>Accepted files&#8230;<\/h2>\n<ul>\n            {this.state.acceptedfiles.map(f => <\/p>\n<li key={f.name}>{f.name} &#8211; {f.size}<\/li>\n<p>)}\n          <\/ul>\n<\/p><\/div>\n<div className=\"uploadFile\">\n<h2>Rejected files&#8230;<\/h2>\n<ul>\n            {this.state.rejectedfiles.map(f => <\/p>\n<li key={f.name}>{f.name} &#8211; {f.size}<\/li>\n<p>)}\n          <\/ul>\n<\/p><\/div>\n<p>        <br className=\"clear\" \/><\/p>\n<div>\n<h2>Preview<\/h2>\n<p>          {prev}<br \/>\n          <br className=\"clear\" \/>\n        <\/div>\n<\/section>\n<p>    )<br \/>\n  }<br \/>\n}<\/p>\n<p>export default Upload<\/p>\n<p>[\/bash]<\/p>\n<h3>\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306e\u5b9f\u88c5<\/h3>\n<p>Rails\u306e\u5b9f\u88c5\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u307e\u305a\u6700\u521d\u306b\u6e21\u3063\u3066\u304d\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc(:name\uff09\u306b\u5f93\u3063\u3066\u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306e\u4f8b\u3067\u306f Tom\u3055\u3093\u56fa\u5b9a\u3067\u3059\u3002<\/p>\n<p>\u3067\u3001file_1\u3001file_2\u3001file_3\u3068\u9023\u756a\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u3066\u304d\u305f\u30d5\u30a1\u30a4\u30eb\u3092<br \/>\n\u305d\u306e\u30d5\u30a9\u30eb\u30c0\u306b\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/p>\n<p>\u306d\u3001\u7c21\u5358\u3067\u3057\u3087\u3002<\/p>\n<p>[bash]<br \/>\n  def upload_image<br \/>\n    image_path = Rails.root.to_s + &#8220;\/public\/upload_images\/&#8221; + params[:name]<br \/>\n    FileUtils.mkdir_p(image_path) unless FileTest.exist?(image_path)<\/p>\n<p>    cnt = 1<br \/>\n    while true do<br \/>\n      break if params[&#8220;file_&#8221; + cnt.to_s].blank?<br \/>\n      file = params[&#8220;file_&#8221; + cnt.to_s]<br \/>\n      fname = file.original_filename<\/p>\n<p>      File.open(image_path + &#8220;\/#{fname}&#8221;, &#8216;wb&#8217;) { |f|<br \/>\n        f.write(file.read)<br \/>\n      }<br \/>\n      cnt += 1<br \/>\n    end<br \/>\n  end<br \/>\n[\/bash]<\/p>\n<h3>\u30c7\u30b6\u30a4\u30f3CSS<\/h3>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001\u9069\u5f53\u3067\u3059\u304c\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u3002<\/p>\n<p>[bash]<br \/>\ndiv.uploadFile{<br \/>\n  float: left;<br \/>\n  width: 260px;<br \/>\n}<\/p>\n<p>div.preview {<br \/>\n  float: left;<br \/>\n  padding: 10px;<br \/>\n  border: 1px solid #cccccc;<br \/>\n}<\/p>\n<p>br.clear {<br \/>\n  clear: both;<br \/>\n}<br \/>\n[\/bash]<\/p>\n<h3>\u3042\u3068\u304c\u304d<\/h3>\n<p>\u51fa\u6765\u4e0a\u304c\u3063\u3066\u307f\u308c\u3070\u3001\u672c\u5f53\u306b\u7c21\u5358\u306b\u51fa\u6765\u3066\u3057\u307e\u3063\u305f\u3088\u3046\u306b\u898b\u3048\u308b\u306e\u3067\u3059\u304c\u30fb\u30fb\u30fb<\/p>\n<p>\u753b\u50cf\u3092POST\u3057\u305f\u6642<br \/>\nRails\u5074\u3067<\/p>\n<p>&#8211; 422 unprocessable entity<br \/>\n&#8211; Invalid form authenticity token<\/p>\n<p>\u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u60a9\u307e\u3055\u308c\u307e\u3057\u305f\u3002CSRF\u306b\u3064\u3044\u3066\u306f\u5206\u304b\u3063\u3066\u308b\u3064\u3082\u308a\u3060\u3063\u305f\u306e\u3067\u3059\u304c\u3001<br \/>\n\u5206\u304b\u3063\u3066\u306a\u304b\u3063\u305f\u3088\u3046\u3067\u3059\u3002<br \/>\nReact\u3067SPA\u3092\u4f5c\u308b\u6642\u306a\u3093\u304b\u306f JS\u3092\u30b5\u30fc\u30d6\u3059\u308b\u30b5\u30fc\u30d0\u30fc\u3068\u3001API\u3092\u30b5\u30fc\u30d6\u3059\u308b\u30b5\u30fc\u30d0\u30fc\u304c<br \/>\n\u5225\u306b\u306a\u308b\u3068\u601d\u3046\u306e\u3067\u3001<\/p>\n<p>gem rack-cors \u3092\u5c0e\u5165\u3057\u3066\u3001\u4fe1\u7528\u3067\u304d\u308b\u30c9\u30e1\u30a4\u30f3\u3092\u7d5e\u308a\u8fbc\u3093\u3067 API\u3092\u53d7\u3051\u4ed8\u3051\u308b\u3002<br \/>\n\u3067\u3001CSRF\u306f ApplicationController\u3067<\/p>\n<p>&#8211; protect_from_forgery with: :null_session<\/p>\n<p>\u3068\u3057\u3066\u3001\u3068\u308a\u3042\u3048\u305a\u4f55\u3067\u3082\u53d7\u3051\u4ed8\u3051\u308b\u3088\u3046\u306b\u3059\u308b\u3002\u3068\u3044\u3046\u611f\u3058\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b React\u3067\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u304f\u3066\u3001\u3069\u306e\u3088\u3046\u306a\u65b9\u6cd5\u304c\u3042\u308b\u306e\u304b\u8abf\u3079\u3066\u307f\u305f\u3002 \u3042\u307e\u308a\u6df1\u304f\u8abf\u3079\u3066\u306f\u306a\u3044\u304c\u3001\uff11\u3064\u3081\u306b\u826f\u3055\u305d\u3046\u3060\u3068\u611f\u3058\u305f react-dropzone \u3092\u4f7f\u3063\u3066\u307f\u305f\u3089\u3001 \u3053\u308c\u304c\u304b\u306a\u308a\u3044\u3044\uff01\uff01 \u3042\u307e\u308a\u306b\u6c17\u306b\u5165\u3063\u3066\u3057\u307e\u3063\u305f\u306e\u3067\u3001react-dropzone\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5bfe\u8c61\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u9078\u3093\u3067\u3001 axios\u3067POST\u3057\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306eRails\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u3056\u3063\u3068\u7d39\u4ecb\u3057\u307e\u3059\u3002 React\u306e\u3053\u3068\u306f\u5206\u304b\u3063\u3066\u3044\u308b\u524d\u63d0\u3067\u3059\u3059\u3081\u307e\u3059\u306e\u3067\u3001\u3042\u3057\u304b\u3089\u305a\u3002 \u51fa\u6765\u4e0a\u304c\u308a\u306e\u30a4\u30e1\u30fc\u30b8\u306f\u3053\u3093\u306a\u611f\u3058 \u753b\u9762\u5de6\u4e0a\u306b \u30d5\u30a1\u30a4\u30eb\u3092\u30c9\u30ed\u30c3\u30d7\u3059\u308b\u5834\u6240\u304c\u3042\u308a\u3001\u305d\u306e\u53f3\u5074\u306b\u30c9\u30ed\u30c3\u30d7\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u5217\u6319\u3059\u308b\u5834\u6240\u304c\u3042\u308b\u3002 \u3067\u3001\u753b\u9762\u4e0b\u90e8\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5bfe\u8c61\u30d5\u30a1\u30a4\u30eb\u306e\u30d7\u30ec\u30d3\u30e5\u30fc\u8868\u793a\u3002 npm \u3067\u5fc5\u8981\u306a\u3082\u306e\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb [bash] $ npm install &#8211;save react-dropzone axios [\/bash] \u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u753b\u9762\u5b9f\u88c5 \u4e00\u6c17\u306b\u8cbc\u308a\u4ed8\u3051\u3061\u3083\u3044\u307e\u3057\u305f\u306e\u3067\u3001\u3056\u3063\u3068\u8aac\u660e\u3057\u3066\u304a\u304d\u307e\u3059\u3002 DropZone \u30bf\u30b0\u3067\u3001\u30c9\u30e9\u30c3\u30b0\u30c9\u30ed\u30c3\u30d7\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3067\u304d\u308b\u6a5f\u80fd\u304c \u3067\u304d\u3061\u3083\u3044\u307e\u3059\u3002accept\u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u53d7\u3051\u4ed8\u3051\u308b\u753b\u50cf\u306e\u7a2e\u985e\uff08\u30d5\u30a1\u30a4\u30eb\u306e\u7a2e\u985e\uff09\u3092\u6307\u5b9a\u3057\u3001 onDrop\u3067\u30c9\u30e9\u30c3\u30b0\u30c9\u30ed\u30c3\u30d7\u3055\u308c\u305f\u6642\u306e\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002 \u4eca\u56de\u306fonDrop\u3067\u53d7\u4ed8\u53ef\u80fd\u306a\u30d5\u30a1\u30a4\u30eb\u3001\u4e0d\u53ef\u80fd\u306a\u30d5\u30a1\u30a4\u30eb\u3092\u5f15\u6570\u3067\u53d7\u3051\u53d6\u308a\u3001 \u30b9\u30c6\u30fc\u30c8\u3092\u5909\u3048\u3066\u3001\u30c9\u30ed\u30c3\u30d7\u9818\u57df\u306e\u53f3\u5074\u306b\u305d\u308c\u3089\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u5217\u6319\u3057\u307e\u3059\u3002 \u3068\u540c\u6642\u306b\u3001\u753b\u9762\u4e0b\u90e8\u306b\u53d7\u4ed8\u53ef\u80fd\u306a\u30d5\u30a1\u30a4\u30eb\u306e\u30d7\u30ec\u30d3\u30e5\u30fc\u3092\u8868\u793a\u3057\u3061\u3083\u3044\u307e\u3059\u3002 \u3053\u308c\u304c\u3044\u3068\u3082\u7c21\u5358\u306a\u3093\u3067\u3059\u3002\u3073\u3063\u304f\u308a\u3067\u3059\u3088\u3002 \u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u30dc\u30bf\u30f3\u304c\u30af\u30ea\u30c3\u30af\u3055\u308c\u305f\u3089\u3001FormData \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u308a\u3001 \u305d\u3053\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5bfe\u8c61\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8a70\u3081\u8fbc\u3093\u3067\u3001axios\u3067POST\u3057\u307e\u3059\u3002 \u305f\u3063\u305f\u3053\u308c\u3060\u3051\u306a\u3093\u3067\u3059\u3088\u3001\u30db\u30f3\u30c8\u4fbf\u5229\u3067\u3059\u306d\u3002 \u4f55\u500b\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u308b\u304b\u306f\u4e0d\u5b9a\u306a\u306e\u3067\u3001 file_1\u3001file_2\u3001file_3\u306e\u3088\u3046\u306a\u9023\u756a\u306e\u540d\u524d\u3092\u3064\u3051\u3066\u304a\u304d\u307e\u3059\u3002 [bash] import React from &#8220;react&#8221; import DropZone from &#8220;react-dropzone&#8221; import axios from &#8220;axios&#8221; import {HOST_NAME} from &#8220;..\/constants\/index&#8221; class Upload extends React.Component { constructor(props){ super(props) this.state = {acceptedfiles: [],rejectedfiles: []} } handleDrop(accepted, rejected) { this.setState({acceptedfiles: accepted, rejectedfiles: rejected}) } handleOpenClick() { this.dropzone.open() } handleUpload() { let data = new FormData() data.append(&#8216;name&#8217;, &#8216;Tom&#8217;) data.append(&#8216;age&#8217;, &#8217;28&#8217;) let cnt = 1 this.state.acceptedfiles.map(file =>{ data.append(&#8216;file_&#8217; + cnt, file) cnt+=1 }) axios.post(HOST_NAME + &#8216;\/upload_image&#8217;, data) } render(){ let prev = [] this.state.acceptedfiles.map(f => prev.push( ) ) return ( Hello Upload&#8230; this.dropzone = node} accept=&#8221;image\/jpg,image\/png&#8221; onDrop={(accepted, rejected)=> this.handleDrop(accepted, rejected)} > \u3053\u3053\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u30c9\u30ed\u30c3\u30d7\u3057\u3066\u304f\u3060\u3055\u3044\u3002 this.handleOpenClick(e)}> Open Dropzone this.handleUpload(e)}> Upload\u5b9f\u884c Accepted files&#8230; {this.state.acceptedfiles.map(f => {f.name} &#8211; {f.size} )} Rejected files&#8230; {this.state.rejectedfiles.map(f => {f.name} &#8211; {f.size} )} Preview {prev} ) } } export default Upload [\/bash] \u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306e\u5b9f\u88c5 Rails\u306e\u5b9f\u88c5\u306b\u306a\u308a\u307e\u3059\u3002 \u307e\u305a\u6700\u521d\u306b\u6e21\u3063\u3066\u304d\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc(:name\uff09\u306b\u5f93\u3063\u3066\u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u308a\u307e\u3059\u3002 \u4eca\u56de\u306e\u4f8b\u3067\u306f Tom\u3055\u3093\u56fa\u5b9a\u3067\u3059\u3002 \u3067\u3001file_1\u3001file_2\u3001file_3\u3068\u9023\u756a\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u3066\u304d\u305f\u30d5\u30a1\u30a4\u30eb\u3092 \u305d\u306e\u30d5\u30a9\u30eb\u30c0\u306b\u4fdd\u5b58\u3057\u307e\u3059\u3002 \u306d\u3001\u7c21\u5358\u3067\u3057\u3087\u3002 [bash] def upload_image image_path = Rails.root.to_s + &#8220;\/public\/upload_images\/&#8221; + params[:name] FileUtils.mkdir_p(image_path) unless FileTest.exist?(image_path) cnt = 1 while true do break if params[&#8220;file_&#8221; + cnt.to_s].blank? file = params[&#8220;file_&#8221; + cnt.to_s] fname = file.original_filename File.open(image_path + &#8220;\/#{fname}&#8221;, &#8216;wb&#8217;) { |f| f.write(file.read) } cnt += 1 end end [\/bash] \u30c7\u30b6\u30a4\u30f3CSS \u3068\u308a\u3042\u3048\u305a\u3001\u9069\u5f53\u3067\u3059\u304c\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u3002 [bash] div.uploadFile{ float: left; width: 260px; } div.preview { float: left; padding: 10px; border: 1px solid #cccccc; } br.clear { clear: both; } [\/bash] \u3042\u3068\u304c\u304d \u51fa\u6765\u4e0a\u304c\u3063\u3066\u307f\u308c\u3070\u3001\u672c\u5f53\u306b\u7c21\u5358\u306b\u51fa\u6765\u3066\u3057\u307e\u3063\u305f\u3088\u3046\u306b\u898b\u3048\u308b\u306e\u3067\u3059\u304c\u30fb\u30fb\u30fb \u753b\u50cf\u3092POST\u3057\u305f\u6642 Rails\u5074\u3067 &#8211; 422 unprocessable entity &#8211; Invalid form authenticity token \u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u60a9\u307e\u3055\u308c\u307e\u3057\u305f\u3002CSRF\u306b\u3064\u3044\u3066\u306f\u5206\u304b\u3063\u3066\u308b\u3064\u3082\u308a\u3060\u3063\u305f\u306e\u3067\u3059\u304c\u3001 \u5206\u304b\u3063\u3066\u306a\u304b\u3063\u305f\u3088\u3046\u3067\u3059\u3002 React\u3067SPA\u3092\u4f5c\u308b\u6642\u306a\u3093\u304b\u306f JS\u3092\u30b5\u30fc\u30d6\u3059\u308b\u30b5\u30fc\u30d0\u30fc\u3068\u3001API\u3092\u30b5\u30fc\u30d6\u3059\u308b\u30b5\u30fc\u30d0\u30fc\u304c \u5225\u306b\u306a\u308b\u3068\u601d\u3046\u306e\u3067\u3001 gem rack-cors \u3092\u5c0e\u5165\u3057\u3066\u3001\u4fe1\u7528\u3067\u304d\u308b\u30c9\u30e1\u30a4\u30f3\u3092\u7d5e\u308a\u8fbc\u3093\u3067 API\u3092\u53d7\u3051\u4ed8\u3051\u308b\u3002 \u3067\u3001CSRF\u306f ApplicationController\u3067 &#8211; protect_from_forgery with: :null_session \u3068\u3057\u3066\u3001\u3068\u308a\u3042\u3048\u305a\u4f55\u3067\u3082\u53d7\u3051\u4ed8\u3051\u308b\u3088\u3046\u306b\u3059\u308b\u3002\u3068\u3044\u3046\u611f\u3058\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n","protected":false},"author":1,"featured_media":1634,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[6,77],"tags":[71,72],"class_list":{"0":"post-1633","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-rails","8":"category-react","9":"tag-rails","10":"tag-reactjs"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/1633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1633"}],"version-history":[{"count":2,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/1633\/revisions"}],"predecessor-version":[{"id":1637,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/posts\/1633\/revisions\/1637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=\/wp\/v2\/media\/1634"}],"wp:attachment":[{"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lovelinux.mydns.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}