1

我正在构建一个页面来管理上传到服务器的文件。

一些客户上传的文件名包含晦涩的德语字符。系统似乎无法正确读取这些,虽然汉字没有问题!

文件名1:1--Referenz Frau Strauß.docx

系统看到的:1--Referenz Frau Strauß.docx

这是我的代码:

protected void gvFiles_RowDeleting(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer;

    TableCell cell = row.Cells[0];
    string fName = cell.Text;
    cell = row.Cells[1];
    string owner = cell.Text;
    if (owner == "-")
    {
        string filePath = "";
        filePath = getFilePath();
        string path = filePath + fName;
        if (File.Exists(path))
        {
            File.Delete(path);
        }
    }
    postbackProc();
}

有问题的字段是 cell.Text。它在屏幕上正确显示,但找不到文件。

我从服务器获取文件名:

private void GetFilesFromDirectory(string DirPath)
{
    try
    {
        DirectoryInfo Dir = new DirectoryInfo(DirPath);
        //Label1.Visible = true;
        lblPath.Visible = true;
        lblPath.Text = DirPath;
        FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories );
        DataTable dt = new DataTable("File_List");
        DataRow dr;
        int iRow = 0;
        dt.Columns.Add("refFileName", typeof(String));
        dt.Columns.Add("Owner", typeof(String));
        foreach (FileInfo FI in FileList )
            {
                iRow++;
                dr = dt.NewRow();
                dr["refFileName"] = FI.Name;
                dr["Owner"] = getFileData(FI.Name);
                dt.Rows.Add(dr);           
            }
        gvFiles.DataSource = dt.DefaultView;
        gvFiles.DataBind();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

有什么解决办法吗?

4

1 回答 1

1

我只能想象你对 utf-8 有问题

确保您的所有服务器端文件(.aspx、.ascx、.html、.cshtml ..etc)都带有(另存为,带编码,utf-8 ..带或不带 bom)

还要检查您的 web.config 以获取正确的 utf 处理

<system.web>
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" responseHeaderEncoding="utf-8" fileEncoding="utf-8" />
</system.web>
于 2013-11-01T20:14:52.917 回答