Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

Get file size from File Upload from IE and Chrome

$
0
0
Hi All,

I am using file upload I am trying get the uploaded file size in client script for IE and Chrome browsers I have been trying for it in the below ways. Can any body please help me with that, I have been with no luck so far. One of this approach worked on another application on the same machine and browser and now its not working when I put this on the original larger application.

Any help may be a code snippet, a link or even a suggestion helps.
 
function CheckImageTypeAndSizeAdd() {
 
    //var aspFileUpload = document.getElementById("fileUploadCurriculumVitaeAdd");
//var errorLabel = document.getElementById("lbl_uploadMessageAdd");
var fileName = $('input[type=file]').val();
    alert(fileName);
 
    var fileSize; //errorLabel.innerHTML = "";
    alert($('<%= fileUploadCurriculumVitaeAdd.ClientID %>').val);
 
   <%-- $(function () {
        $('<%= fileUploadCurriculumVitaeAdd.ClientID %>').change(function () {
 
            //because this is single file upload I use only first index
var f = this.files[0]
            fileSize = f.size;
            //here I CHECK if the FILE SIZE is bigger than 8 MB (numbers below are in bytes)
if (f.size > 8388608 || f.fileSize > 8388608) {
                //show an alert to the user
                alert("Allowed file size exceeded. (Max. 8 MB)")
 
                //reset file upload control
this.value = null;
            }
        })
    });--%> 
    try {
        fileSize = aspFileUpload.files[0].size; // Size returned in bytes.
    } catch (e) {
        var objFSO = new ActiveXObject("Scripting.FileSystemObject");
        var e = objFSO.getFile(fileName);
        fileSize = e.size;
    }
    alert(fileSize);
    var ext = fileName.substr(fileName.lastIndexOf('.') + 1).toLowerCase();
    if (!(ext == "docx" || ext == "doc" || ext == "pdf")) {
        errorLabel.innerHTML = "Invalid file type, must select a *.doc, *.docx, or *.pdf file.";
        $('input[type=file]').val() = "No file chosen";
        return false;
    }
    if (fileSize == -1) {
        errorLabel.innerHTML = "Couldn't load doc file size.  Please try to save again.";
        $('input[type=file]').val() = "No file chosen";
        return false;
    }
    else if (fileSize <= 2097152) {errorLabel.innerHTML= fileName;
returntrue;}else{varfileSize= (fileSize /1048576);errorLabel.innerHTML= "File is too large, must select file under 20 Mb. File  Size: "+fileSize.toFixed(1).toString()+"Mb";$('input[type=file]').val()= "No file chosen";returnfalse;}}
 
Thanks,

Abdul Aleem

"There is already enough hatred in the world lets spread love, compassion and affection."

Viewing all articles
Browse latest Browse all 3938

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>