douzhaiyuan1731 2009-11-29 21:36
浏览 24
已采纳

图像上传 - 在点击提交之前在网页上显示图像

To better learn web development, I'm trying to write a PHP page that will let me upload and download files from a server, which I've done in ASP before.

I am however, tripping over something that reason says should be simple, but I'm not able to find an answer for it.

Here's the pretty standard code for the browse/submit:

<form enctype="multipart/form-data" action="uploadFile.php" method="POST"/>  
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />  
<input type="file" name="pix" size="100" />  
</form>

Now when you hit the "Browse.." button on the resulting web page, you get the standard file dialog and then return to the page with the correct file path in the text box. (AND, my uploadfile.php works just fine when I click Submit.)

What I'd like to do is actually SHOW the user (well, me) the picture on the web page if it is an IMG-suitable file or else a placeholder picture for non-image files -- BEFORE the user clicks the SUBMIT button.

Note the correct pathname for the client-side file is RIGHT there on the page. I cannot for the life of me figure out how to capture it from the DOM, or for that matter figure out how to get JavaScript to notice the return-with-correct-pathname in order to trigger showing the picture.

(I apologize for the broadness of this question -- I did try quite a bit of searching through previous questions and answers here, but when I'm lacking possibly not only the technical terms but the CONCEPTS it's hard to frame a question.)

  • 写回答

6条回答 默认 最新

  • dongmin4990 2009-11-30 04:49
    关注

    You can do this with Firefox and other browsers that support the FileList API.

    var myImage = new Image(),
    myImage.src = myFileInput.files[0].getAsDataURL();
    

    Edit: The max file size should not be specified on the client side as it can be exploited but you can at least verify it on the client side using files[0].getAsBinary().length.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题