.Net multipate / form-data form enctype and UTF-8 "special" characters => (MVC w / HttpPostedFileBase)
Purpose:
Download / publish the CSV file with UTF-8 characters to the MVC action, read the data and paste it into the database table.
Problem:
Scroll only characters of plain text. UTF-8 "special" characters, such as á, do not pass correctly in the code and in the database, which they display as this character =>.
Additionally:
I am convinced that this is not a problem with my C # code, although I have included the important parts below.
I thought the problem was that the downloaded file was encoded as plain text or as plain / text MIME, but I was able to change it by changing the file extension to .html
Summary:
How do you get a form with the enctype attribute set to "multipart / form-data" to correctly interpret the UTF-8 characters in the published file?
Study:
From my research, this seems to be a common problem without a common and clear solution.
I found more solutions for java and PHP than .NET.
The csvFile variable is of type HttpPostedFileBase
this is an MVC action signature
[HttpPost]
public ActionResult LoadFromCsv(HttpPostedFileBase csvFile)
Things I tried:
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)
When a file has a .txt extension, the ContentType property for HttpPostedFileBase is "text / plain"
When I change the file extension from .txt to .csv, the ContentType property for HttpPostedFileBase is "application / vnd.ms-excel"
When I change the file extension to .html, the ContentType property for HttpPostedFileBase is "text / html" - I thought it would be a winner, but it is not.
, . , , UTF-8 - ! ?!?!
, mime IIS -?
, DOCTYPE/html tag/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--
, , - .
, :
CSV Excel, .
.
<form method="post" action="@Url.Action("UploadFile", "Home")" enctype="multipart/form-data">
<input type="file" id="file" name="file" />
<input type="submit" />
</form>
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
using (StreamReader reader = new StreamReader(file.InputStream, System.Text.Encoding.UTF8))
{
string text = reader.ReadToEnd();
}
return RedirectToAction("Index");
}
, - .
"", , , , " ", "ANSI". UTF-8 , , .