Home  >  Article  >  Web Front-end  >  asp.net js implements code for uploading and parsing csv files without refreshing_javascript skills

asp.net js implements code for uploading and parsing csv files without refreshing_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:061151browse

I used it at work a while ago. I pasted the code and only kept the code related to uploading. I found that the code was actually very small.
Upload page html/js

Copy code The code is as follows:

















< /td>



< ;/td>





 
C# for processing file uploads
Copy code The code is as follows:

if (context.Request.Files.Count > 0)
{
HttpPostedFile file = context.Request.Files[0];
if (file.ContentLength > 0)
{
string title = string.Empty;
title = DateTime.Now.ToString("yyyyMMddHHmmss") "_" Path.GetFileName(file.FileName);
string path = "./Upload/" title;
path = System.Web.HttpContext.Current.Server.MapPath(path);
file.SaveAs(path);
context.Response.Write("<script>window.parent.FinishUpload('" HttpUtility.UrlEncode(path) "');</script>");
}
}
else
{
context.Response.Write("<script>window. parent.FinishUpload('');</script>");
}

The basic principle to achieve no refresh is: submit the form to the iframe, so that the refresh occurs in iframe. The form sets the action to point to processing the uploaded file and the target to the iframe. The result of the upload operation can be returned to the iframe, and the FinishUpload method of the parent object is called to display whether the upload is successful. Therefore, when AJAX was not popular, this method was often used to disguise the unrefreshed effect, and it can still be used now.
It should be noted that:
enctype="multipart/form-data" is essential, the default encoding of enctype is "application/x-www-form-urlencoded", set enctype="multipart/form-data", Used to upload files in binary mode.
To prevent garbled file names and problems in URL transmission with garbled characters, perform HttpUtility.UrlEncode when returning the file name to the front desk, and perform decodeURIComponent when fetching the file name in the front desk JS.
When resetting the file selection box, you need to temporarily insert the file selection box into the temporary form and reset it through the reset method of the form.
By the way, paste the code for parsing the uploaded csv file
Parse the csv file
Copy the code The code is as follows:

private DataTable ImportDataTable(string filepath)
{
DataTable mydt = new DataTable("myTableName");
mydt.Columns.Add("Data ID", System.Type.GetType("System.String"));
mydt.Columns.Add("Field Name", System.Type.GetType("System.String"));
mydt.Columns.Add("New Value", System.Type.GetType("System.String"));
DataRow mydr;
using (System.IO.StreamReader mysr = new System.IO.StreamReader(filepath))
{
int data;
char current;
StringBuilder text = new StringBuilder();
IDictionary> results = new Dictionary>();
bool isInYinHao = false; ;
int lineId = 1;
int index = 0;
while (true)
{
data = mysr.Read();
if (data != -1)
{
current = (char)data;
if (current == '"')
{
if (isInYinHao)
{
isInYinHao = false;
}
else
{
if (index > 0)
{
text.Append(current);
}
isInYinHao = true;
}
}
else if (current == ',')
{
if (isInYinHao)
{
text.Append(current);
}
else
{
SaveResult(results, lineId, text);
index = 0;
continue;
}
}
else if (current == 'r')
{
if (isInYinHao)
{
text.Append(current);
}
}
else if (current == 'n')
{
if (isInYinHao)
{
text.Append(current);
}
else
{
SaveResult(results, lineId, text);
index = 0;
lineId ;
continue;
}
}
else if (current == '
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn