简体   繁体   English

通过Javascript进行Html文件上传预览

[英]Html file upload preview by Javascript

I want to show a PREVIEW kind of thing for an post , so took details by JS 我想为一篇文章展示一个PREVIEW类的东西,所以JS的细节

but problem comes when it comes to <input type="file" , it's not giving full path to the file 但问题来自于<input type="file" ,它没有提供文件的完整路径

Ex: if I do 例如:如果我这样做

$("#image").val();

it only give "Sunset.jpg" not C:\\Documents and Settings\\All Users....\\Sunset.jpg 它只给“Sunset.jpg”而不是C:\\ Documents and Settings \\ All Users .... \\ Sunset.jpg

any idea how to get that detail value? 任何想法如何获得细节价值?

Although, as others have already pointed out, you cannot learn the full path to the file in JavaScript, perhaps a completely different approach to the problem might still work for you. 虽然正如其他人已经指出的那样,你无法在JavaScript中学习文件的完整路径,但是对于这个问题,完全不同的方法可能仍然适合你。

You could upload the photo automatically as soon as the visitor has picked it (the behavior you see in GMail, among other applications), so it's residing on your server even as the visitor continues interacting with your page. 您可以在访问者选择它后立即自动上传照片(您在GMail中看到的行为以及其他应用程序),因此即使访问者继续与您的页面进行交互,它也会驻留在您的服务器上。 At that point, showing a preview is as simple as serving one yourself from your own (now server-side) copy of the image. 此时,显示预览就像从您自己(现在服务器端)的图像副本自己提供预览一样简单。

This if for security reasons, so you cannot read files from the users system using JavaScript. 这是出于安全原因,因此您无法使用JavaScript从用户系统中读取文件。

If you happen find a workaround, there will probably be security patches released by browser vendors sooner rather than later. 如果您找到解决方法,浏览器供应商可能会尽快发布安全补丁。 I know because in earlier versions if IE, it was possible to read the full path and hence display a preview, at least if the file was an image. 我知道,因为在IE的早期版本中,可以读取完整路径并因此显示预览,至少如果文件是图像。 I used that in a CMS UI, but of course that nifty feature was ruined by an IE service release :-/ 我在CMS用户界面中使用过它,但当然这个漂亮的功能被IE服务版本破坏了: - /

In general the file upload control is somewhat of a "black box" for security reasons. 通常,出于安全原因,文件上载控件在某种程度上是“黑盒子”。 You have only very limited access to scripting and styling it. 您只能非常有限地访问脚本和样式。 This is so you can't snoop or upload files without the user knowing, or so you cannot trick the user into uploading files with a deceptive interface. 这样您就无法在用户不知情的情况下窥探或上传文件,或者因此您无法欺骗用户使用欺骗性界面上传文件。

This is now possible as of Firefox 3.6 https://developer.mozilla.org/en/DOM/FileList 现在可以从Firefox 3.6 https://developer.mozilla.org/en/DOM/FileList开始

Though the code they show does not seem to work in Chrome, so I'm guessing even the HTML5 JavaScript API for things like this has yet to be standardized. 虽然他们展示的代码似乎在Chrome中不起作用,但我猜这个类似的HTML5 JavaScript API还没有标准化。

This is a security feature. 这是一项安全功能。 You cannot change or get the entire path in secure browsers, so what you want to do, cannot be done. 您无法在安全的浏览器中更改或获取整个路径,因此无法完成您想要做的事情。 If you use something like Flash, Silverlight or Java, then it should be relatively easy. 如果你使用像Flash,Silverlight或Java这样的东西,那么它应该相对容易。

As far as I know, there is no way to do what you want to do in javascript. 据我所知,没有办法在javascript中做你想做的事情。 Its a pretty big security issue - having filesystem access in a browser. 这是一个非常大的安全问题 - 在浏览器中访问文件系统。

There are a few browser plugins (java/flash/gears) that may allow you to do this sort of thing. 有一些浏览器插件(java / flash /齿轮)可能允许你做这种事情。 I know with gears you can get the user to select a file - and open it to read the content using JS. 我知道使用齿轮可以让用户选择一个文件 - 并打开它以使用JS读取内容。

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

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