欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net+js 實(shí)現(xiàn)無(wú)刷新上傳解析csv文件的代碼

 更新時(shí)間:2010年05月17日 23:50:11   作者:  
無(wú)刷新上傳解析csv文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
前陣子工作中用到,貼上代碼,僅保留上傳有關(guān)的代碼,發(fā)現(xiàn)code其實(shí)很少。
上傳頁(yè)面html/js
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script type="text/javascript">
function FinishUpload(filePath) {
document.getElementById("uploadForm").reset();
if (!filePath) {
alert("Import Failed!");
}
else {
alert("Imported Successfully to " + decodeURIComponent(filePath) + "!");
}
}
function UploadFile() {
var arr = document.getElementById("txtFile").value.split('.');
var fileType = arr[arr.length - 1];
if (fileType.toLowerCase().indexOf("csv") < 0) {
document.getElementById("uploadForm").reset();
alert("Please select a csv file.");
return false;
}
document.getElementById("uploadForm").encoding = "multipart/form-data";
document.getElementById("uploadForm").submit();
}
function ResetFile(file) {
var tmpForm = document.createElement('form');
file.parentNode.insertBefore(tmpForm, file);
tmpForm.appendChild(file);
tmpForm.reset();
tmpForm.removeNode(false);
}
</script>
</head>
<body>
<form id="uploadForm" name="uploadForm" action="Upload.ashx" method="post" target="hidIframe" enctype="multipart/form-data">
<table cellpadding='0' cellspacing='0' style="width:100%;height:100%;border-collapse:collapse;" border="0" >
<tr>
<td>
<input id="txtFile" name="txtFile" type="file" style="border:solid 1px Gray;" />
<iframe name="hidIframe" id="hidIframe" style="display:none;" ></iframe>
</td>
</tr>
<tr>
<td>
<input type="button" id="btnImportOK" value="Upload" onclick="UploadFile();" />
<input type="button" id="btnImportCancel" onclick="ResetFile(document.getElementById('txtFile'))" value="Reset"/>
</td>
</tr>
</table>
</form>
</body>
</html>

 
處理文件上傳的c#
復(fù)制代碼 代碼如下:

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>");
}

  做到無(wú)刷新,基本原理就是:通過(guò)表單提交到iframe里,從而使刷新發(fā)生在iframe里。form設(shè)置action指向處理上傳的文件,target指向iframe。上傳操作的結(jié)果可以返回到iframe里,調(diào)用父對(duì)象的FinishUpload方法顯示是否上傳成功。所以在AJAX未流行時(shí),常用這種方法來(lái)偽裝未刷新的效果,現(xiàn)在仍然可以使用。
要注意的是:
enctype="multipart/form-data"不可少,enctype默認(rèn)編碼是“application/x-www-form-urlencoded”,設(shè)置enctype="multipart/form-data",用于二進(jìn)制方式上傳文件。
為防止文件名亂碼,防止亂七八糟字符URL傳遞中出現(xiàn)問(wèn)題,返回文件名到前臺(tái)時(shí)進(jìn)行HttpUtility.UrlEncode,在前臺(tái)JS中取文件名時(shí)進(jìn)行decodeURIComponent。
重置文件選擇框時(shí),要把文件選擇框臨時(shí)插入到臨時(shí)表單,通過(guò)表單的reset方法重置。
順便貼上解析上傳的csv文件的code
解析csv文件
復(fù)制代碼 代碼如下:

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<int, List<string>> results = new Dictionary<int, List<string>>();
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 == '\0')
{
}
else
{
text.Append(current);
}
index++;
}
else
{
//Read to file end.
SaveResult(results, lineId, text);
break;
}
}
foreach (int id in results.Keys)
{
mydr = mydt.NewRow();
for (int i = 0; i < results[id].Count; i++)
{
if (i > 2)
{
break;
}
mydr[i] = results[id][i];
}
mydt.Rows.Add(mydr);
}
}
return mydt;
}
private void SaveResult(IDictionary<int, List<string>> results, int lineId, StringBuilder text)
{
if (!results.ContainsKey(lineId))
{
results.Add(lineId, new List<string>());
}
results[lineId].Add(text.ToString());
text.Remove(0, text.Length);
}

點(diǎn)擊下載

相關(guān)文章

  • 基于javascript實(shí)現(xiàn)貪吃蛇經(jīng)典小游戲

    基于javascript實(shí)現(xiàn)貪吃蛇經(jīng)典小游戲

    這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 關(guān)于js遍歷表格的實(shí)例

    關(guān)于js遍歷表格的實(shí)例

    js可以利用dom非常輕松的就可以遍歷一個(gè)表格。當(dāng)然只要是dom中有的所有對(duì)象都可以通過(guò)js來(lái)訪問(wèn)和處理
    2013-07-07
  • H5實(shí)現(xiàn)仿flash效果的實(shí)現(xiàn)代碼

    H5實(shí)現(xiàn)仿flash效果的實(shí)現(xiàn)代碼

    這篇文章主要介紹了H5實(shí)現(xiàn)仿flash效果的實(shí)現(xiàn)代碼的相關(guān)資料,希望通過(guò)本文能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-09-09
  • JavaScript中string對(duì)象

    JavaScript中string對(duì)象

    和JAVA中String對(duì)象用處一樣啊,javascript中的String對(duì)象也有自己的方法,和JAVA中的類似。
    2015-06-06
  • JS截取字符串常用方法詳細(xì)整理

    JS截取字符串常用方法詳細(xì)整理

    截取字符串的使用比較廣泛,有很多中方法,本文粗略的整理了一些,感興趣的額朋友可以才參考下
    2013-10-10
  • 淺談js算法和流程控制

    淺談js算法和流程控制

    代碼整體結(jié)構(gòu)是執(zhí)行速度的決定因素之一,代碼量少不一定運(yùn)行速度快,代碼量多也不一定運(yùn)行速度慢。本篇文章將對(duì)js的算法和流程控制進(jìn)行介紹,下面跟著小編一起來(lái)看下吧
    2016-12-12
  • 一起學(xué)寫js Calender日歷控件

    一起學(xué)寫js Calender日歷控件

    這篇文章主要和大家一起學(xué)寫js Calender控件,自己動(dòng)手編寫了一個(gè)簡(jiǎn)易日歷控件,感興趣的小伙伴們可以參考一下
    2016-04-04
  • 微信小程序wx.request的簡(jiǎn)單封裝

    微信小程序wx.request的簡(jiǎn)單封裝

    這篇文章主要介紹了微信小程序wx.request的簡(jiǎn)單封裝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 原生js基于canvas實(shí)現(xiàn)一個(gè)簡(jiǎn)單的前端截圖工具代碼實(shí)例

    原生js基于canvas實(shí)現(xiàn)一個(gè)簡(jiǎn)單的前端截圖工具代碼實(shí)例

    這篇文章主要介紹了原生js基于canvas實(shí)現(xiàn)一個(gè)簡(jiǎn)單的前端截圖工具代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • JavaScript實(shí)現(xiàn)下拉列表框數(shù)據(jù)增加、刪除、上下排序的方法

    JavaScript實(shí)現(xiàn)下拉列表框數(shù)據(jù)增加、刪除、上下排序的方法

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)下拉列表框數(shù)據(jù)增加、刪除、上下排序的方法,涉及javascript針對(duì)頁(yè)面下拉列表框元素的添加、刪除及移動(dòng)的相關(guān)技巧,具有一定參考借鑒,需要的朋友可以參考下
    2015-08-08

最新評(píng)論