.Net multipart/form-data 表单 enctype 和 UTF-8 "special"个字符 => � (MVC w/HttpPostedFileBase)

标签 .net asp.net-mvc file-upload utf-8 httppostedfilebase

目标:

将带有 UTF-8 字符的 CSV 文件上传/发布到 MVC 操作,读取数据并将其粘贴到数据库表中。

问题:

只有纯文本字符才能通过。 UTF-8“特殊”字符(如 á)无法正确显示,在代码和数据库中它们呈现为该字符 => �。

更多:

我确信这不是我的 C# 代码的问题,尽管我已经包含了下面的重要部分。

我认为问题在于上传的文件被编码为纯文本或“纯文本”MIME 类型,但我可以通过将文件扩展名更改为 .html 来更改它

摘要:

如何获取 enctype 属性设置为“multipart/form-data”的表单,以正确解释已发布文件中的 UTF-8 字符?

研究:

根据我的研究,这似乎是一个常见问题,没有通用且明确的解决方案。

我还发现了比 .Net 更多的 Java 和 PHP 解决方案。

<小时/>
  • csvFile 变量的类型为 HttpPostedFileBase

  • 这是 MVC 操作签名

[HttpPost]

公共(public) ActionResult LoadFromCsv(HttpPostedFileBase csvFile)

<小时/>

我尝试过的事情:

1)

using (Stream inputStream = csvFile.InputStream)
{
    byte[] bytes = ReadFully(inputStream);
    string bytesConverted = new UTF8Encoding().GetString(bytes);
}

2)

using (Stream inputStream = csvFile.InputStream)
{
    using (StreamReader readStream = new StreamReader(inputStream, Encoding.UTF8, true))
    {
        while (!readStream.EndOfStream)
        {
            string csvLine = readStream.ReadLine();
            // string csvLine = new UTF8Encoding().GetString(new UTF8Encoding().GetBytes(readStream.ReadLine())); // stupid... this can not be the way!
        }
    }
}

3)

<form method="post" enctype="multipart/form-data" accept-charset="UTF-8">

4)

<input type="file" id="csvFile" name="csvFile" accept="UTF-8" />

<input type="file" id="csvFile" name="csvFile" accept="text/html" />

5)

当文件具有.txt扩展名时,HttpPostedFileBase的ContentType属性为“text/plain”

当我将文件扩展名从 .txt 更改为 .csv 时,HttpPostedFileBase 的 ContentType 属性为“application/vnd.ms-excel”

当我将文件扩展名更改为 .html 时,HttpPostedFileBase 的 ContentType 属性为“text/html” - 我以为这会成为赢家,但事实并非如此。

<小时/>

在我内心深处,我必须相信这个问题有一个简单的解决方案。令我惊讶的是,我自己无法解决这个问题,在文件中上传 UTF-8 字符是一项常见任务!为什么我在这里失败了?!?!

也许我必须在 IIS 中为网站调整 MIME 类型?

也许我需要不同的 DOCTYPE/html 标签/元标签?

<小时/>

@Gabe -

这是我的帖子在 fiddler 中的样子。这真的很有趣,因为 � 很简单,就在帖子值中。

http://localhost/AwesomeGeography/GeoBytesCities/LoadFromCsv?adsf HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost/AwesomeGeography/GeoBytesCities/LoadFromCsv?adsf
Content-Type: multipart/form-data; boundary=---------------------------199122566726299
Content-Length: 354

-----------------------------199122566726299
Content-Disposition: form-data; name="csvFile"; filename="cities_test.html"
Content-Type: text/html

"CityId","CountryID","RegionID","City","Latitude","Longitude","TimeZone","DmaId","Code"
3344,10,1063,"Luj�n de Cuyo","-33.05","-68.867","-03:00",0,"LDCU"
-----------------------------199122566726299--

最佳答案

我也有同样的问题,你可以使用

StreamReader reader = new StreamReader(archivo_origen.InputStream, Encoding.GetEncoding("iso-8859-1"));

它可以工作,“iso-8859-1”适用于拉丁衍生语言,例如西类牙语、aleman、frances

关于.Net multipart/form-data 表单 enctype 和 UTF-8 "special"个字符 => � (MVC w/HttpPostedFileBase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10872034/

相关文章:

c# - 连接到 DirectX 应用程序

c# - 无法从 IIS(内部服务器 500)使用 Visual Studio 2015 运行简单的 ASP.NET MVC 应用程序

r - 获取上传文件的名称作为Shiny中的变量

c# - 从IOS图片上传到.net app : Rotate

file-upload - Amazon S3 crossdomain.xml 始终显示 403 Forbidden

.net - 回滚 .net 事务的最佳方法是什么?

.net - 是asp.net和.net框架版本相同?还是asp.net或clr版本都一样?

c# - Web 服务不再适用于单元测试/客户端代理创建

jquery - 如何从 Kendo Grid 数据源将对象发送到 Controller ?

c# - ASP.NET MVC4 - Session_End - 如何在 Global.asax 的 Session_End 中获取当前登录的用户名?