Gulrej
Gulrej

Reputation: 999

Server.map path not working in asp.net

I am using this code to download a excel file which exist in my solution. I have added a folder FileUpload and added a excel file UploadCWF.xlsx. My code is workin in local host. But not working when I host this to server.I am getting error - Could not find a part of the path. My code -

        string filePath = HttpContext.Current.Server.MapPath("~/FileUpload/");
        string _DownloadableProductFileName = "UploadCWF.xlsx";

        System.IO.FileInfo FileName = new System.IO.FileInfo(filePath + "\\" + _DownloadableProductFileName);
        FileStream myFile = new FileStream(filePath + "\\" + _DownloadableProductFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

        //Reads file as binary values
        BinaryReader _BinaryReader = new BinaryReader(myFile);

        //Check whether file exists in specified location
        if (FileName.Exists)
        {
            try
            {
                long startBytes = 0;
                string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
                string _EncodedData = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp;

                Response.Clear();
                Response.Buffer = false;
                Response.AddHeader("Accept-Ranges", "bytes");
                Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
                Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name);
                Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString());
                Response.AddHeader("Connection", "Keep-Alive");
                Response.ContentEncoding = Encoding.UTF8;

                //Send data
                _BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);

                //Dividing the data in 1024 bytes package
                int maxCount = (int)Math.Ceiling((FileName.Length - startBytes + 0.0) / 1024);

                //Download in block of 1024 bytes
                int i;
                for (i = 0; i < maxCount && Response.IsClientConnected; i++)
                {
                    Response.BinaryWrite(_BinaryReader.ReadBytes(1024));
                    Response.Flush();
                }
            }
            catch (Exception es)
            {
                throw es;
            }
            finally
            {
                Response.End();
                _BinaryReader.Close();
                myFile.Close();
            }
        }
        else

            System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(),
            "FileNotFoundWarning", "alert('File is not available now!')", true);

Please some one help me.

Upvotes: 0

Views: 6393

Answers (1)

user1990587
user1990587

Reputation: 386

You should first concat filepath and filename then get path using server.mappath.

You should write code like this

string filePath = HttpContext.Current.Server.MapPath("~/FileUpload/UploadCWF.xlsx");

 System.IO.FileInfo FileName = new System.IO.FileInfo(filePath);

Upvotes: 4

Related Questions