为什么这段代码没有给我正确的图片上传尺寸(Why this code isn't giving me correct dimensions of image about to be uplo

编程入门 行业动态 更新时间:2024-10-25 18:24:32
为什么这段代码没有给我正确的图片上传尺寸(Why this code isn't giving me correct dimensions of image about to be uploaded)

我有一个图片的上传表单。 当用户选择一个文件时,我需要在上传之前在客户端使用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.

更多推荐

本文发布于:2023-04-28 00:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:给我   这段   图片上传   尺寸   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!