简体   繁体   English

HTML表单使用JavaScript从url图像上传文件

[英]HTML form upload file from url image using JavaScript

Some background information:一些背景信息:

Users are able to input some book information and upload book cover image file through HTML form.用户可以通过 HTML 格式输入一些书籍信息并上传书籍封面图像文件。 Before they input information manually, they could use Google Book API to find some information, and by just click a button, some fields will be automatically filled using the information from Google Book API.在手动输入信息之前,他们可以使用 Google Book API 查找一些信息,只需单击一个按钮,一些字段就会使用来自 Google Book API 的信息自动填充。 For book cover image, Google API returns the image url, so I am trying find a way to transfer this url to a file object.对于书籍封面图片,Google API 返回图片 url,因此我试图找到一种方法将此 url 传输到文件对象。 Therefore, when users click the button to fill form using information from Google API, the image will be automatically uploaded there from image url.因此,当用户点击按钮使用来自 Google API 的信息填写表单时,图像将自动从图像 url 上传到那里。

I have an image url such as: http://books.google.com/books/content?id=L2byvgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api我有一个图片网址,例如:http: //books.google.com/books/content ?id=L2byvgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api

And I created an HTML form that allows users to upload their images.我创建了一个 HTML 表单,允许用户上传他们的图片。

<div class="form-group">
        <label for="book cover">Book Cover</label>
        <input type="file" class="form-control" name="book cover" accept="image/*" required>
    </div>

Now I want to use JavaScript to transfer the image url to a file object and then prefill this file input for some users.现在我想使用 JavaScript 将图像 url 传输到文件对象,然后为某些用户预填充此文件输入。

How can I achieve this using JavaScript?如何使用 JavaScript 实现此目的? Thanks!谢谢!

For your case, I think you must save the link to input hidden and submit to server side to get that image.对于您的情况,我认为您必须保存输入隐藏的链接并提交到服务器端以获取该图像。 If you want to show the preview for user, just create a <img src=" http://books.google.com/books/content?id=L2byvgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api" /> on your form.如果你想为用户显示预览,只需在你的表单上创建一个<img src=" http://books.google.com/books/content?id=L2byvgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api" />

Put hidden input to keep the link from user:放置隐藏输入以防止用户链接:

<input type="hidden" name="image-url" value="http://books.google.com/books/content?id=L2byvgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api">

<img src="http://books.google.com/books/content?id=L2byvgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api"/>


And then, you have to take action download this image on your server side.然后,您必须采取行动在您的服务器端下载此图像。

Please use below code to make it working:请使用以下代码使其工作:

Function功能

function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function(e) {
      $('#blah').attr('src', e.target.result);
    }

    reader.readAsDataURL(input.files[0]);
  }
}

$("#imgInp").change(function() {
  readURL(this);
});

HTML Form view HTML 表单视图

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form runat="server">
  <input type='file' id="imgInp" />
  <img id="blah" src="#" alt="your image" />
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM