is it possible to preview local images before uploading them via a form?

前端 未结 6 977
不思量自难忘°
不思量自难忘° 2020-12-02 21:07

To be more specific, I want to use a form with one or more file input fields used for images. When those fields are changed, I\'d like to show a preview of the associated im

6条回答
  •  一整个雨季
    2020-12-02 21:36

    The first step is finding out the image path. JavaScript is allowed to interrogate the upload control for a filename/path, but (for reasons of security) various browsers show different things to the JS engine than they display to the user - they tend to keep the filename intact so you can at least validate its extension, but you may get c:\fake_path\ or some similarly obfuscated thing prepended to the filename. Trying this on various browsers will give you an idea as to what gets returned as a real path, and what gets faked out, and where.

    The second step is displaying the image. It's possible to display local images if you know their paths, via img tags with file:// source URLs, if the user's browser allows the file:// scheme. (Firefox doesn't, by default.) So if you can get the user to tell you what the full path to the image is, you can at least try to load it.

提交回复
热议问题