2016-08-24 14 views
1

私はユーザーが画像をアップロードしてフォームを送信するための簡単なフォームを作成しています。フォームデータはGoogleスプレッドシートにアップロードされます。画像がGoogleスプレッドシートに表示されるようにするには、画像に独自のURLが必要です。私はimgurのAPIをテストしていますが、このウォークスルーの後では、https://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/を使ってもうまく動作します。生成されたリンクURLをテキストフィールドに入力する

私のフォームはその特定のイメージのURLを生成しました。どのように私のグーグルスプレッドシートにそのURLを設定しますか?私は生成されたときにhrefを自動入力するテキストボックスを使用することを考えていましたが、それを行う方法はわかりません。

imgurのAPIでリンクが生成された後、スプレッドシートに簡単にリンクを貼り付けることはできますか?

私は、ここでデモを行っています。

function upload(file) { 
 
     /* Is the file an image? */ 
 
     if (!file || !file.type.match(/image.*/)) return; 
 
     /* It is! */ 
 
     document.body.className = "uploading"; 
 
     /* Lets build a FormData object*/ 
 
     var fd = new FormData(); 
 
     fd.append("image", file); // Append the file 
 
     var xhr = new XMLHttpRequest(); // Create the XHR 
 
     xhr.open("POST", "https://api.imgur.com/3/image.json"); // Boooom! 
 
     xhr.onload = function() { 
 
      // Big win! 
 
      document.querySelector("#link").href = JSON.parse(xhr.responseText).data.link; 
 
      document.body.className = "uploaded"; 
 
     } 
 
     
 
     xhr.setRequestHeader('Authorization', 'Client-ID 490be95195679b1'); // Imgur API key here 
 
     
 
     
 
     /* And now, we send the formdata */ 
 
     xhr.send(fd); 
 
    }
#link, p , div {display: none} 
 
    .uploading div {display: none} 
 
    .uploaded div {display: none} 
 
    .uploading p {display: inline} 
 
    .uploaded #link {display: inline}
<input type="file" onchange="upload(this.files[0])"><br /> 
 
<!-- this is the text box we could paste the url in --> 
 
<input tyle="text"> 
 

 
<p>Uploading...</p> 
 
<a id="link">Link to imgur file</a>

答えて

1

これはURLで

document.getElementById("urlText").value = JSON.parse(xhr.responseText).data.link; 

<input type="text" id="urlText" name="urlText"> 

完全なコードテキストボックスに移入されます:

function upload(file) { 
 
     /* Is the file an image? */ 
 
     if (!file || !file.type.match(/image.*/)) return; 
 
     /* It is! */ 
 
     document.body.className = "uploading"; 
 
     /* Lets build a FormData object*/ 
 
     var fd = new FormData(); 
 
     fd.append("image", file); // Append the file 
 
     var xhr = new XMLHttpRequest(); // Create the XHR 
 
     xhr.open("POST", "https://api.imgur.com/3/image.json"); // Boooom! 
 
     xhr.onload = function() { 
 
      // Big win! 
 
      document.querySelector("#link").href = JSON.parse(xhr.responseText).data.link; 
 
      document.body.className = "uploaded"; 
 
     document.getElementById("urlText").value = JSON.parse(xhr.responseText).data.link; 
 
     } 
 
     
 
     xhr.setRequestHeader('Authorization', 'Client-ID 490be95195679b1'); // Imgur API key here 
 
     
 

 
     /* And now, we send the formdata */ 
 
     xhr.send(fd); 
 
    }
#link, p , div {display: none} 
 
    .uploading div {display: none} 
 
    .uploaded div {display: none} 
 
    .uploading p {display: inline} 
 
    .uploaded #link {display: inline}
<input type="file" onchange="upload(this.files[0])"><br /> 
 
<input type="text" id="urlText" name="urlText"> 
 

 
<p>Uploading...</p> 
 
<a id="link">Link to imgur file</a>

+0

働いていました!ありがとうございました! – cgrouge

+0

私は

アップロードすることができますどのような方法です...

独自のクラスとまだロードされ、アップロード時に表示されますか?私はすべてのpタグを非表示にしたくないのです。私のページの他の情報を隠すからです。 – cgrouge

+0

また、2つの別々のファイル入力に対してこの作業を行うにはどうすればよいですか? – cgrouge

関連する問題