当我使用javascript的输入(type = file)时如何获取文件大小 [英] How do I get the filesize when I use javascript's input(type=file)

查看:65
本文介绍了当我使用javascript的输入(type = file)时如何获取文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我使用javascript在项目中获取文件大小。



如果用户的客户端是chrome,firefox等,我将使用以下代码:

  //  这个obj是值输入(类型=文件) 
// 此代码可以在chrome,firefox,等等。
var fileSize = obj.files [ 0 ]。size;





如果用户的客户端是IE,IE的版本< = IE9,我将使用以下代码:

  //  此代码可以在> = IE10  
// 如果在< = IE10中运行,浏览器将抛出异常:
// Aumation服务器无法创建对象
// 我知道我可以在互联网设置中做一些事情
/ / 来解决这个问题。
// 但我不想这样使用。
// 因为我们可以实际上不允许用户这样做,
// 有些用户可能会认为他妈的这个网站是什么?
// 为什么我必须为您的网站进行设置?
var filePath = obj;
var FSO = new ActiveXObject( Scripting.FileSystemObject);
var file = FSO.GetFile(filePath);
var fileSize = file.size;





我在javascript中找到一个对象,它是image。然后我试一试:

  //  不幸的是无法访问dynsrc属性。(IE的版本< = IE9) 
var image = new Image();
image.dynsrc = obj; // obj = filePath
var fileSize = image.fileSize;

// 我再试一次。
// 使用大写字母表示dynsrc
// 像这样:
image.DYNSRC = obj;
// 浏览器不会再次抛出异常!
// 但WTF!
// < span class =code-comment>此SHIT无法访问属性fileSize.....> _< .......





我不想使用flash对象。有些神能帮帮我吗?

PLEAS E ......

解决方案

基本上你无法从JavaScript访问文件系统 - 为此你有无法访问大小等文件属性...

但是,使用HTML5(仅限HTML5!),您可以访问一些属性(无害的属性),包括大小...



HTML

 <   input     type   =   file    id   =  idFile    /  >  





jQuery(JavaScript)

 


' #idFile')。bind(' 更改'功能(){
alert( this .files [ 0 ]。size);
});





此代码当然仅支持现代浏览器(如果IE需要10或更高) ...



至于图像 - 无法获得图像背后的文件大小。图像对象只有一个与尺寸相关的信息 - 尺寸......


Hello everyone.I use the javascript to get filesize in a project.

If user's client is chrome,firefox,etc.I will use the follow code:

//this "obj" is the value of input(type=file)
//this code can run perfect in chrome,firefox,etc.
var fileSize=obj.files[0].size;



If user's client is IE ,and IE's version <= IE9,I will use the follow code:

//this code can run in >= IE10
//If run in <= IE10,the browser will throw a exception:
//Aumation server cannot create object
//I know I can do something in the Internet Settings
//to solve this problem.
//but I donot want to use this way.
//Because we can not let users to do this in fact,
//and some users maybe think that what the f u c k about this website?
//Why I must do the settings for your website?
var filePath=obj;
var FSO=new ActiveXObject("Scripting.FileSystemObject");
var file=FSO.GetFile(filePath);
var fileSize=file.size;



I find a object in javascript ,it is "image".Then I have a try:

//Unfortunately can not visit the property "dynsrc".(IE's version <= IE9)
var image=new Image();
image.dynsrc=obj;//obj=filePath
var fileSize=image.fileSize;

//I have another try.
//Use capital letters for "dynsrc"
//Like this:
image.DYNSRC=obj;
//The browser does not throw exception again!
//but WTF!
//This S H I T can't visit the property "fileSize".....>_<.......



I don't want to use flash object.Can some gods help me ?
P L E A S E......

解决方案

Basically you have no access to the file system from JavaScript - and for that you have no access to file properties like size...
However, with HTML5 (and only with HTML5!) you have access to some properties (the harmless ones), including size...

HTML

<input type="file" id="idFile" />



jQuery (JavaScript)


('#idFile').bind('change', function() { alert(this.files[0].size); });



This code of course supported only on modern browsers (in case if IE you need 10 or better)...

As for images - it is impossible to get the file size behind the image. The image object has only one information related to size - the dimensions...


这篇关于当我使用javascript的输入(type = file)时如何获取文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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