我可以使用任何 HTML 或 JavaScript API 来获取 input[type=file] 中的文件路径吗? [英] Can I use any HTML or JavaScript API to get the file's path in input[type=file]?

查看:24
本文介绍了我可以使用任何 HTML 或 JavaScript API 来获取 input[type=file] 中的文件路径吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个上传图片的功能.

I want to implement an upload image function.

上传后,我想获取文件本地路径,以便我可以创建缩略图.但是我怎样才能得到文件路径呢?还有其他方法可以创建缩略图吗?

After uploading, I want get the file local path so I can create a thumbnail. But how can I get the file path? Is there another way to create the thumbnail?

我尝试过 jQuery-File-Upload 插件.

I have tried jQuery-File-Upload plugin.

推荐答案

我认为 chrome 支持这个,但出于安全原因不确定它是否被删除.无论如何你可以尝试使用 javascrip 设置一些 div 的自定义宽度和高度并使用上传文件的 readAsDataURL

I think chrome supports this but not sure whether it is removed due to security reasons.Anyways you can try playing with javascrip to set custom width and height of some div and use readAsDataURL of the uploaded file

 <script type="text/javascript">     

function uploadFile(input) {         

if (input.files && input.files[0]) {            

 var reader = new FileReader();              
 reader.onload = function (e) {                 
 $('#test').attr('src', e.target.result).width(100).height(100);             
 };               
 reader.readAsDataURL(input.files[0]);        
 }     
} 

</script> 

<input type='file' onchange="uploadFile(this);" />     <img id="test" src="#" alt="my image" /> 

这篇关于我可以使用任何 HTML 或 JavaScript API 来获取 input[type=file] 中的文件路径吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆