locked
get file (pdf) size RRS feed

  • Question

  •  

    Hi I have a pdf file located on the server "http://........./test.pdf

     

    I am tring to get the size of the file. in bytes.

     

    I did this, but it gives me an error..

     

    FileInfo finfo = new FileInfo(http://.../test.pdf);

    Response.Write(finfo.Length.ToString());

     

    It works if I put the file on my local drive (C:\\test.pdf") but It doesnt work if I want to read it from the server.

     

    What am I missing?

     

    Thank you.

     

    Friday, July 13, 2007 2:35 PM

Answers

  • I'm not sure if all http servers give this information, but you can try doing what they did in this article: http://www.codeproject.com/useritems/downloadFileswProgressbar.asp,

     

    In C# the relevant lines would be:

    Code Snippet

    try

    {

        HttpWebRequest request = WebRequest.Create(@"http:/......./test.pdf");

        HttpWebResponse response = request.GetResponse();

        long length = response.ContentLength;

    }

    catch

    {

        // handle exception

    }

     

    I'm not sure if it actually downloads the file here...you'll want to try that out.

    Friday, July 13, 2007 2:57 PM
  • I have to use Active mode in some of my ftp requests, I use the following code, It may work better for you pass in the absolute path to your ftp file and set passive to false

     

    public string GetFileSize(string RemoteFile, bool PassiveMode)

    {

    string result = string.Empty;

    try

    {

    // Get the object used to communicate with the server.

    Uri ftpUri = new Uri(UriEncode(RemoteFile));

    //Setup request using Uri

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUri);

    request.Method = WebRequestMethods.Ftp.GetFileSize;

    request.Credentials = new NetworkCredential(Uid, Pwd);

    request.UsePassive = PassiveMode;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    result = response.ContentLength.ToString();

    }

    catch (Exception ex)

    {

    throw ex;

    }

    return result;

    }

     

    Hope this helps

    Thursday, July 26, 2007 11:46 PM

All replies

  • I'm not sure if all http servers give this information, but you can try doing what they did in this article: http://www.codeproject.com/useritems/downloadFileswProgressbar.asp,

     

    In C# the relevant lines would be:

    Code Snippet

    try

    {

        HttpWebRequest request = WebRequest.Create(@"http:/......./test.pdf");

        HttpWebResponse response = request.GetResponse();

        long length = response.ContentLength;

    }

    catch

    {

        // handle exception

    }

     

    I'm not sure if it actually downloads the file here...you'll want to try that out.

    Friday, July 13, 2007 2:57 PM
  • It worked Smile Thanks,

     

    But now I think the site is loading slower.. and gets timed out sometimes.

     

    Do you know if there is another way.

    Friday, July 13, 2007 3:42 PM
  •  

    I tried to do this, But it doesn't work.. Any ideas?

     

    string loc = Request.ApplicationPath+"/foldername/"+file; (pdf file)

    if(File.Exists(loc))

    {

    ......

    }

     

    but it doesn't find.

     

    Thanks,

     

    Friday, July 13, 2007 6:14 PM
  • I have to use Active mode in some of my ftp requests, I use the following code, It may work better for you pass in the absolute path to your ftp file and set passive to false

     

    public string GetFileSize(string RemoteFile, bool PassiveMode)

    {

    string result = string.Empty;

    try

    {

    // Get the object used to communicate with the server.

    Uri ftpUri = new Uri(UriEncode(RemoteFile));

    //Setup request using Uri

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUri);

    request.Method = WebRequestMethods.Ftp.GetFileSize;

    request.Credentials = new NetworkCredential(Uid, Pwd);

    request.UsePassive = PassiveMode;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    result = response.ContentLength.ToString();

    }

    catch (Exception ex)

    {

    throw ex;

    }

    return result;

    }

     

    Hope this helps

    Thursday, July 26, 2007 11:46 PM