在 InputFile 组件中获取变音符号作为符号

Getting diacritic character as symbols in InputFile component

提问人:Durai Murugan 提问时间:4/10/2023 更新时间:4/10/2023 访问量:59

问:

读取具有变音符号字符的 zip 包文件名(例如:TestDocumentáááá)时,
它会返回如下所示的符号字符。

实际结果:TestDocument .docx 预期结果:TestDocumentááá.docx

实际结果


    private async void OnInputFileChange(InputFileChangeEventArgs e)
    {
        foreach (IBrowserFile file in e.GetMultipleFiles())
        {
            await using Stream stream = file.OpenReadStream((int)file.Size);
            MemoryStream memoryStream = new MemoryStream((int)file.Size);
            await stream.CopyToAsync(memoryStream);

            memoryStream.Position = 0;
            
            CancellationTokenSource cancellation = new CancellationTokenSource();
            byte[] buffer = new byte[file.Size];
            string UniqueFileName = Path.GetRandomFileName() + ".bin";
            string tmpfile = Path.GetTempFileName();
            tmpfile = e.File.Name;

            int bytesRead = 0;
            while ((bytesRead = await memoryStream.ReadAsync(buffer, cancellation.Token)) != 0)
            {
                using MemoryStream tmpMemoryStream = new MemoryStream(buffer, 0, bytesRead);

                using (var fs = new FileStream(tmpfile, FileMode.Append))
                {
                    tmpMemoryStream.WriteTo(fs);
                }
            };

            using ZipArchive zipArchive = ZipFile.OpenRead(tmpfile);
            foreach (ZipArchiveEntry entry in zipArchive.Entries)
            {
                // Get file name as Symbolioc character
                // entry.FullName ->> TestDocument���.docx

                //byte[] bytes = Encoding.UTF8.GetBytes(entry.FullName);
                //string text2 = Encoding.UTF8.GetString(bytes);
            }
        }
    }
C# UTF-8 Blazor BlazorInputFile

评论


答:

2赞 Klaus Gütter 4/10/2023 #1

看起来像编码问题。 您可能希望使用此 ZipArchive 构造函数,而不是可以显式指定编码的地方,例如:ZipFile.OpenRead

using var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read, false, 
    Encoding.GetEncoding("IBM437");

较旧的 ZIP 文件通常以 IBM437 编码,因此请尝试指定为 encoding。Encoding.GetEncoding("IBM437")

请注意,如果您使用的不是 .NET Framework 4.x,则需要在应用程序启动时添加(添加 nuget 包 System.Text.Encoding.CodePages)。Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)

评论

0赞 Graffito 4/10/2023
也可以尝试拉丁语1:Encoding.GetEncoding("ISO-8859-1")