我有一个图片的上传表单。 当用户选择一个文件时,我需要在上传之前在客户端使用js / jquery获取图像的宽度和高度。
我使用此代码来监视用户何时从本地计算机上选择要上传的图像
$('#upload').change(function(){ if($(this).val() != null){ getImgSize(this); } });它调用这个函数,但它警告宽度和高度都是0
function getImgSize(imgSrc){ var newImg = new Image(); newImg.src = imgSrc; var height = newImg.height; var width = newImg.width; alert ('The image size is '+width+'*'+height); }这是html标记
<input name="upload" id="upload" type="file">I have an upload form for an image. When user selects a file, I need to get the image width and height with js/jquery on the client side before upload.
I'm using this code to monitor when the user selects the image they want to upload from their local computer
$('#upload').change(function(){ if($(this).val() != null){ getImgSize(this); } });and it calls this function, but it alerts that the width and height are 0
function getImgSize(imgSrc){ var newImg = new Image(); newImg.src = imgSrc; var height = newImg.height; var width = newImg.width; alert ('The image size is '+width+'*'+height); }and this is the html markup
<input name="upload" id="upload" type="file">最满意答案
有一条规则,即客户端JavaScript代码无法与客户端文件系统联系。 因此,您无法从输入中获取所选文件的完整路径。 它只会返回文件的名称,这对于您的计划不适用。
There is a rule, that a client side javascript code cannot reach out to the clients file system. Because of this, you cannot get the full path of the selected file, from the input. It would only return the name of the file, which is not enough for your plan to work.
更多推荐
发布评论