Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all , I am building a web application
i want to upload image in my application , but
My program has a limited volume
i want to get file size before upload ,
If file size is greater than the amount,the program will fail
So , How can i get file size before upload in C# ?
:) tnx All
Posted

Hi

Before upload we cannot get the value in server side .
We have to go for plugin or client side code .

If you are using IIS 7.0 refer below link .

http://msdn.microsoft.com/en-us/library/ms689462%28VS.90%29.aspx[^]
 
Share this answer
 
If you are using Asp File Upload Control than You can use PostedFile.ContentLength property of File Upload Control.


For detailed info, Kindly have a look at following.

MSDN--FileUploadControl.PostedFile.ContentLength Property

Hope it will help. :)
 
Share this answer
 
You will need javascript code for that .You can write a function on javascript which will send the file size after uploading a file if file upload .

<asp:fileupload id="fu_Upload" runat="server" onchange="javascript:getFileSize(this);" xmlns:asp="#unknown">

function getFileSize(input) {
var file = input.files[0];
return file.size;
}
 
Share this answer
 
try this
C#
using System;
using System.IO;

class Program
{
    static void Main()
    {
    // The name of the file
    const string fileName = "test.txt";

    // Create new FileInfo object and get the Length.
    FileInfo f = new FileInfo(fileName);
    long s1 = f.Length;

    // Change something with the file.
    File.AppendAllText(fileName, " More characters.");

    // Create another FileInfo object and get the Length.
    FileInfo f2 = new FileInfo(fileName);
    long s2 = f2.Length;

    // Print out the length of the file before and after.
    Console.WriteLine("Before and after: " + s1.ToString() +
        " " + s2.ToString());

    // Get the difference between the two sizes.
    long change = s2 - s1;
    Console.WriteLine("Size increase: " + change.ToString());
    }
}
 
Share this answer
 
Comments
Zoltán Zörgő 24-Oct-13 6:59am    
Not relevant to this question, even less because OP asked how to limit uploaded file size in a web application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900