locked
Gibberish file names when trying to download files with hebrew chrachters RRS feed

  • Question

  • User682240180 posted

    First : sorry for my english !


    Hello everyone.
    In My project I use  links for downloading files from the local disk.

    The problem is : files with Hebrew names show Gibberish .


    My function for downloading is :


            protected void DownloadFile(string filePath)
    {
    try
    {
    HttpContext.Current.Response.ContentType = "application/octet-stream";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(filePath));
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.WriteFile(filePath);
    HttpContext.Current.Response.End();
    }
    catch(Exception ex)
    {
    HttpContext.Current.Response.End();
    //Save Error
    sbl.InsertError(userDeptId, DateTime.Now, userId, GetCurrentPageName(), GetFunctionName(), ex.Message);

    }
    }

    Example



    Saturday, August 14, 2010 2:45 PM

Answers

  • User1389397289 posted

    I believe I got the solution of your problem.
    For reproducing your problem I made a temp. xlsx file on my localhost with using Hebrew characters(כשא.xlsx).And used the following code for saving the Hebrew characters file.
    I am getting the correct name for saving the file as file name is not converted to the Gibberish characters.

    The key line of the code is getting the file name as below:


    string filename = fileInfo.Name;
    filename = HttpUtility.UrlPathEncode(filename);
     

    Code:

    Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1255"); 
                                                                                                         //for Hebrew
    Response.HeaderEncoding = System.Text.Encoding.GetEncoding("windows-1255");
                                                                                                        //for Hebrew
                    string filepath = fileUpload.PostedFile.FileName.ToString();
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath);
                    string filename = fileInfo.Name;
                    filename = HttpUtility.UrlPathEncode(filename);
                    Response.Clear();
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" +
                                                                                                           filename);
                    
    
                    Response.AppendHeader("Content-Length", fileInfo.Length.ToString()); 
                                                                     //Sends only the File with no Extra bytes
    
                    Response.Flush();
    
                    Response.WriteFile(filepath);
                    Response.Flush(); //Release the file so it can be Deleted




    Please Mark As Answer if It works for you

    Regards,
    MKhare




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 16, 2010 6:55 AM