【发布时间】:2012-06-07 23:29:41
【问题描述】:
目标:
将带有 UTF-8 字符的 CSV 文件上传/发布到 MVC 操作,读取数据并将其粘贴到数据库表中。
问题:
只有纯文本字符才能通过。像 á 这样的 UTF-8“特殊”字符在代码和数据库中没有正确通过,它们呈现为这个字符 => �。
更多:
我确信这不是我的 C# 代码的问题,尽管我已经包含了下面的重要部分。
我认为问题在于上传的文件被编码为纯文本或“纯/文本”MIME 类型,但我可以通过将文件扩展名更改为 .html 来更改它
总结:
如何获得 enctype 属性设置为“multipart/form-data”的表单以正确解释已发布文件中的 UTF-8 字符?
研究:
根据我的研究,这似乎是一个没有通用且明确的解决方案的常见问题。
我发现 java 和 PHP 的解决方案也比 .Net 多。
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 标签 / meta 标签?
@Gabe -
这是我的帖子在提琴手中的样子。这真的很有趣,因为 � 很简单,就在 post 值中。
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--
【问题讨论】:
-
您使用的是 SQL Server 数据库吗?检查其排序规则。您可以通过here了解更多信息。
-
#1 是我认为可行的。如果没有,我会检查网络嗅探器(或者可能是 Fiddler),以验证正确的字节是否正在向服务器发送。
-
@lucask - 当我使用 MS sql 服务器导入向导时,UTF-8 字符会进入数据库,所以它不是数据库。这 ?字符存在于 C# 值中,因此它在 db 插入之前存在。
-
@Gabe - 这是我的帖子在提琴手中的样子。
-
(提琴手发布数据,并在问题中添加了字符)
标签: .net asp.net-mvc file-upload utf-8 httppostedfilebase