用生成的链接url填充文本字段

Populate text field with generated link url

本文关键字:填充 文本 字段 url 链接      更新时间:2023-09-26

我正在创建一个简单的表单,允许用户上传图像并提交表单。表单数据被上传到google电子表格中。为了使图像在谷歌电子表格上填充,我需要图像有自己的url。我正在测试imgur的API,它按照下面的步骤运行得很好:https://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/.

现在,我的表单为特定的图像生成了一个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>

这会用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>