为什么IE(8)显示错误的FileName?创建文件时

本文关键字:FileName 创建 文件 错误 IE 显示 为什么 | 更新日期: 2023-09-27 18:26:12

我写这个程序,但不能在IE(8)等浏览器中工作**为什么IE(8)显示错误的FileName**换句话说,它将更改为unicode名称!

当使用显式名称时,它可以正确工作,但当我使用"fileName"时,它不能正确工作我发现我的源代码

请帮帮我非常感谢

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <hr />
            <asp:Button ID="btnEncrypt" Text="Encrypt File" runat="server" OnClick="EncryptFile" />
            <asp:Button ID="btnDecrypt" Text="Decrypt File" runat="server" OnClick="DecryptFile" />
            <br />
            <input type="submit" value="ersal" name="test" />
        </div>
    </form>
</body>
</html>

C# code is Here


  protected void EncryptFile(object sender, EventArgs e)
    {
        //Get the Input File Name and Extension.
        string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
        string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
        //Build the File Path for the original (input) and the encrypted (output) file.
        string input = Server.MapPath("~/Files/") + fileName + fileExtension;
        string output = Server.MapPath("~/Files/") + fileName + "_enc" + fileExtension;
        //Save the Input File, Encrypt it and save the encrypted file in output path.
        FileUpload1.SaveAs(input);
        this.Encrypt(input, output);
        ////Download the Encrypted File.
        Response.ContentType = FileUpload1.PostedFile.ContentType;
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(output));
        Response.WriteFile(output);
        Response.Flush();
        //Delete the original (input) and the encrypted (output) file.
        File.Delete(input);
        File.Delete(output);
        Response.End();
    }
    private void Encrypt(string inputFilePath, string outputfilePath)
    {
        string EncryptionKey = "A";
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
            {
                using (CryptoStream cs = new CryptoStream(fsOutput, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
                    {
                        int data;
                        while ((data = fsInput.ReadByte()) != -1)
                        {
                            cs.WriteByte((byte)data);
                        }
                    }
                }
            }
        }
    }

    protected void DecryptFile(object sender, EventArgs e)
    {
        //Get the Input File Name and Extension
        string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
        string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
        //Build the File Path for the original (input) and the decrypted (output) file
        string input = Server.MapPath("~/Files/") + fileName + fileExtension;
        string output = Server.MapPath("~/Files/") + fileName + "_dec" + fileExtension;
        //Save the Input File, Decrypt it and save the decrypted file in output path.
        FileUpload1.SaveAs(input);
        this.Decrypt(input, output);
        //Download the Decrypted File.
        Response.Clear();
        Response.ContentType = FileUpload1.PostedFile.ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(output));
        Response.WriteFile(output,false);
        Response.Flush();
        //Delete the original (input) and the decrypted (output) file.
        File.Delete(input);
        File.Delete(output);
        Response.End();
    }
    private void Decrypt(string inputFilePath, string outputfilePath)
    {
        string EncryptionKey = "A";
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
            {
                using (CryptoStream cs = new CryptoStream(fsInput, encryptor.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
                    {
                        int data;
                        while ((data = cs.ReadByte()) != -1)
                        {
                            fsOutput.WriteByte((byte)data);
                        }
                    }
                }
            }
        }
    } 

只有Chrome显示正确的结果!

为什么IE(8)显示错误的FileName?创建文件时

问题是Internet Explorer处理包含Unicode字符的文件名的方式与Chrome等其他浏览器不同。不幸的是,没有一个简单的跨浏览器解决方案来解决这个问题。

我们已经使用本问题中讨论的解决方案解决了这个问题:如何在HTTP中对Content-Disposition标头的文件名参数进行编码?