2023年1月15日
(转)js 获取本地上传的文件(图片和视频)的宽高和大小
选择的文件类型 e.target.files[0].type 值
/** * e.target.files[0].type的结果 * .PSD 文件 "" * .psd 文件 "" * .pdf 文件 "application/pdf" * .PNG 文件 "image/png" * .png 文件 "image/png" * .JPEG 文件 "image/jpeg" * .jpg 文件 "image/jpeg" */
1. 不限制上传的图像或视频的格式:
/** 不限制上传的图像或视频的格式 */ <input type="file" accept="image/*" multiple /> <input type="file" accept="video/*" multiple />
2. 显示上传的文件类型在一定范围内:
/** 限制上传的文件类型在一定范围内 */ <input type="file" accept="image/png, image/jpeg" multiple /> <input type="file" accept="image/png, image/jpeg, video/mp4" multiple />
一、获取选择的本地的图片的宽高
/**
* 上传图片
* @param e 选择的文件对象
* @param progressCallback 进度回调
* @returns {Promise<any>}
*/
function uploadImageFile(e, progressCallback) {
return new Promise(function (resolve, reject) {
var fileObj = e.target.files[0];
if (fileObj.type != "image/png" && fileObj.type != "image/jpeg") {
console.log("请上传正确的文件类型");
reject("");
}
if (fileObj.size > 5*1024*1024) {
console.log("图片文件过大");
reject("");
}
// 获取上传的图片的宽高
var reader = new FileReader();
reader.readAsDataURL(fileObj);
reader.onload = function (evt) {
var replaceSrc = evt.target.result;
var imageObj = new Image();
imageObj.src = replaceSrc;
imageObj.onload = function () {
console.log(imageObj.width + imageObj.height);
// 执行上传的方法,获取外网路径,上传进度等
resolve();
};
};
});
}
二、获取上传的本地视频的宽高
/**
* 上传视频
* @param e 选择的文件对象
* @param progressCallback 进度回调
* @returns {Promise<any>}
*/
function uploadVideoFile(e, progressCallback) {
return new Promise(function (resolve, reject) {
var fileObj = e.target.files[0];
if (fileObj.type !== "video/mp4") {
console.log("请上传正确的文件类型");
reject("");
}
if (fileObj.size > 200*1024*1024) {
console.log("视频文件过大");
reject("");
}
// 获取上传的视频的宽高
var videoUrl = URL.createObjectURL(fileObj);
var videoObj = document.createElement("video");
videoObj.onloadedmetadata = function (evt) {
URL.revokeObjectURL(videoUrl);
console.log(videoObj.videoWidth + videoObj.videoHeight);
// 执行上传的方法,获取外网路径,上传进度等
resolve();
};
videoObj.src = videoUrl;
videoObj.load();
});
}
三、限制上传的PDF文件
/**
* 上传PDF
* @param e 选择的文件对象
* @param progressCallback 进度回调
* @returns {Promise<any>}
*/
function uploadPdfFile(e, progressCallback) {
return new Promise(function (resolve, reject) {
var fileObj = e.target.files[0];
if (fileObj.type !== "application/pdf") {
console.log("请上传正确的文件类型");
reject("");
}
if (fileObj.size > 60*1024*1024) {
console.log("PDF文件过大");
reject("");
}
// 执行上传的方法,获取外网路径,上传进度等
resolve();
});
}
acne meds rx [acnemedsrx.com]
I’m curious to find out what blog system you are working with?
I’m experiencing some minor security issues with my latest site
and I’d like to find something more secure. Do you have
any recommendations?
If you are going for most excellent contents like I do, simply visit this
site all the time because it presents feature contents, thanks
I read this piece of writing completely on the topic of the difference of most recent and earlier technologies,
it’s awesome article.
Thanks for sharing your thoughts on gaya hidup sehat powered by blogger.
Regards
An outstanding share! I have just forwarded this onto a colleague who was conducting a little homework on this.
And he actually ordered me lunch simply because I discovered
it for him… lol. So let me reword this…. Thank YOU for
the meal!! But yeah, thanks for spending the time to discuss this matter here on your blog.
Thank you for the auspicious writeup. It in fact was a
amusement account it. Look advanced to more added agreeable from you!
By the way, how could we communicate?
Hi there! I know this is somewhat off topic but I was wondering if you knew where I could
find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one?
Thanks a lot!
Aw, this was a really good post. Taking a few minutes and actual effort to generate a great article… but what can I say… I
hesitate a whole lot and never manage to get nearly anything done.
I am regular reader, how are you everybody? This article posted at
this site is really good.
Wonderful goods from you, man. I’ve have in mind your stuff previous to and you’re
simply extremely fantastic. I really like what you have got right here, really like what you
are stating and the best way by which you say it. You make it entertaining and you
still care for to stay it smart. I cant wait to read much more from you.
This is actually a terrific site.
It’s going to be ending of mine day, except before ending
I am reading this wonderful post to improve my know-how.
I think this is one of the most important information for me.
And i’m glad reading your article. But want to
remark on some general things, The site style is perfect,
the articles is really excellent : D. Good job, cheers
Hi, yeah this article is actually pleasant and I have learned lot of things from
it concerning blogging. thanks.
Hi there! This is kind of off topic but I need some advice from an established blog.
Is it hard to set up your own blog? I’m not very
techincal but I can figure things out pretty fast.
I’m thinking about making my own but I’m not sure where to begin. Do
you have any tips or suggestions? Many thanks
What a material of un-ambiguity and preserveness of precious knowledge about unexpected feelings.
If you are going for finest contents like me,
only pay a quick visit this web site all the time for
the reason that it gives quality contents, thanks
Премиальные букеты отличаются особым стилем и качеством исполнения.
На странице https://million-buketov.ru/premium-bukety представлены лучшие композиции.
Они создаются из отборных цветов.
Такой подарок подчеркивает статус
и внимание. Это идеальный вариант для важных событий.
Magnificent beat ! I wish to apprentice while you amend your site, how can i subscribe for a weblog web site?
The account helped me a applicable deal. I have been tiny bit familiar of
this your broadcast provided vivid transparent idea
Simply desire to say your article is as amazing. The clarity in your post
is simply great and i could assume you are an expert on this subject.
Well with your permission allow me to grab your feed to keep up to date with forthcoming post.
Thanks a million and please carry on the enjoyable work.
I do not know whether it’s just me or if perhaps everyone else encountering problems with your website.
It appears as if some of the written text in your content are running off the screen. Can someone else please provide feedback and let
me know if this is happening to them as well?
This may be a issue with my browser because I’ve had this happen previously.
Thanks
Do you have a spam problem on this website; I also am
a blogger, and I was wanting to know your situation;
we have created some nice practices and we are looking to exchange solutions
with others, please shoot me an email if interested.
I was recommended this website by my cousin. I am not sure whether this post is written by him as
no one else know such detailed about my problem.
You are incredible! Thanks!
Do you have a spam issue on this blog; I also am a blogger, and I was wondering your situation; many of us
have developed some nice practices and we are looking to swap methods with other folks, please shoot me an e-mail if interested.
When I originally left a comment I appear to have clicked on the -Notify me
when new comments are added- checkbox and now each time a comment is added I recieve four emails with the exact same
comment. Is there a means you can remove me from that service?
Thanks!
This site certainly has all the info I wanted concerning
this subject and didn’t know who to ask.
I read this post fully regarding the difference of newest and
preceding technologies, it’s amazing article.
Thank you for the good writeup. It in fact was
a amusement account it. Look advanced to more added
agreeable from you! By the way, how can we
communicate?
Awesome issues here. I’m very satisfied to see your
article. Thank you a lot and I am having a look forward to touch you.
Will you kindly drop me a e-mail?
I must thank you for the efforts you have put in writing this blog.
I really hope to check out the same high-grade blog
posts by you later on as well. In truth, your creative writing abilities
has inspired me to get my very own blog now 😉
I every time used to read piece of writing in news papers
but now as I am a user of net therefore from now I am using net for content, thanks to web.
I’m gone to convey my little brother, that he should
also visit this weblog on regular basis to get updated
from newest news update.
Terrific article! That is the type of information that are meant to be shared around the net.
Shame on the search engines for now not positioning this publish higher!
Come on over and consult with my site . Thank you =)
You’re so awesome! I don’t suppose I’ve read a single thing like this before.
So wonderful to discover another person with unique thoughts on this subject.
Really.. many thanks for starting this up.
This website is something that is needed on the internet, someone with some originality!
Have you ever thought about adding a little bit more than just your articles?
I mean, what you say is fundamental and everything. But imagine if you added some great visuals or
video clips to give your posts more, “pop”!
Your content is excellent but with pics and clips, this website could undeniably be one of the greatest in its field.
Fantastic blog!
If you want to get much from this post then you have to apply these methods to
your won webpage.
I am extremely impressed together with your
writing talents and also with the format in your
blog. Is that this a paid subject or did you modify
it yourself? Anyway stay up the nice high quality
writing, it is rare to peer a nice blog like this one these days..