Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have written the following code.

C#
public void ProcessRequest(HttpContext context)
{
    try
    {
        if (context.Session != null)
        {
            if (context.Session["CurrentUser"] != null)
            {
                string urlRequested = HttpUtility.UrlDecode(context.Request.Url.AbsolutePath);
                string filePath = HttpContext.Current.Server.MapPath(urlRequested);

                FileInfo thisFileInfo = new FileInfo(filePath);
                string fileExtention = thisFileInfo.Extension;

                context.Response.AddHeader("Content-Length", thisFileInfo.Length.ToString());

                //set content type
                DocumentTypeService documentTypesService = new DocumentTypeService();
                DocumentType currentType = documentTypesService.ReadByExtension(fileExtention.Replace(".", "").ToLower());

                if (fileExtention != null)
                {
                    context.Response.ContentType = currentType.ContentType;
                }
                context.Response.TransmitFile(filePath);
                context.ApplicationInstance.CompleteRequest();
                //context.Response.End();
            }
            else
            {
                context.Response.Redirect("~/Login.aspx");
            }
        }
        else
        {
            context.Response.Redirect("~/Login.aspx");
        }
    }
    catch
    {
        context.Response.Redirect("~/Login.aspx");
    }
}

When I tried to execute this code I am getting 'FileNotFound' exception in the line
C#
context.Response.AddHeader("Content-Length", thisFileInfo.Length.ToString());

in some case and in some cases it is moving to the catch statement.

Can anyone help me to fix this issue as it is causing many other issue in my projects which are related to this.
Posted
v2
Comments
F-ES Sitecore 10-Dec-15 8:40am    
Have you debugged? What is in urlRequested? What is in filePath? Is it a valid path and does a file exist at that path? You might need to add a "~/" to the front of the urlRequested variable. We can't run your code or access your file system.

http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn
user 3008 10-Dec-15 9:01am    
Thanks for your quick response.

Actually the files are stored in database and the project is stored locally in my system. The urlrequest displays "/DocumentRepository/Schools/4687/Logos/8994.gif"
which is the url as per the sql, where 4687 is the schoolID as per the database record and I have issue in loading the logo of the school. The filePath shows "C:\User\vasur\Desktop\Solution\Cds.Tfl.Stars.Wed\Document\Schools\4687\Logos\8994.gif" is the path where my project is stored locally my project is stored locally and the file does not exist in that local path as it is stored in the database.

I even tried by including ~/ to the front of urlrequest but that didn't have any effect.
Richard Deeming 10-Dec-15 9:33am    
So the file does not exist, and you're confused as to why you're getting a "file not found" exception when you try to read its length?!

The code you have shown is for transmitting a file which exists on disk. If your file is stored in the database, then you'll need to read it from the database, set the response properties based on the data, and write the data to the response. Using FileInfo and Response.TransmitFile will not work.
Sinisa Hajnal 11-Dec-15 2:52am    
Or you could get the file from the database, save it to your local path and then transmit. But then you'll have to have read/write access to some local folder (which is fine while you're testing, but hardly sure thing on hosting servers).
Sudheer Kumar Tiwari 14-Dec-15 23:36pm    
First save file from your local drive then fetch from there
your string filePath is not returning any values. P'haps its null.

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