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 представлены лучшие композиции.
Они создаются из отборных цветов.
Такой подарок подчеркивает статус
и внимание. Это идеальный вариант для важных событий.