ASP.NET WebAPI導(dǎo)入CSV
一、前端代碼
<button type="button" class="btn btn-primary" onclick="InportTicket()">導(dǎo)入</button> <input id="fileToUpload" type="file" name="upfile" style="display:none;">
/// JS腳本
$("#fileToUpload").click();
$("#fileToUpload").change(function () {
var formData = new FormData();
formData.append("myfile", document.getElementById("fileToUpload").files[0]);
$.ajax({
url: "../Ticket/TicketFileToUpload",
type: 'POST',
cache: false,
processData: false,
contentType: false,
data: formData,
success: function (res) {
alert(res.Message);
},
error: function (data, status, e) {
alert("操作失敗!");
}
})
});二、后臺(tái)實(shí)現(xiàn)代碼
[HttpPost]
public ActionResult TicketFileToUpload()
{
try
{
if (Request.Files.Count > 0)
{
HttpPostedFileBase TicketFile = Request.Files[0];
List<string[]> lstData = Helper.ImportExport.InportData(TicketFile.InputStream);
TicketModel ticketope = new TicketModel();
for (int i = 1; i < lstData.Count; i++)
{
string[] itemData = lstData[i];
Ticket entity = ticketope.GetByCode(itemData[0]);
if (entity == null)
{
entity = new Ticket();
entity.Label = itemData[1];
entity.SiteId = int.Parse(itemData[2]);
entity.Owner = itemData[4];
entity.CardId = itemData[5];
entity.StartDate = DateTime.Parse(itemData[6]);
entity.EndDate = DateTime.Parse(itemData[7]);
entity.IsValid = bool.Parse(itemData[8]);
entity.IsUsed = bool.Parse(itemData[9]);
ticketope.Insert(entity);
}
}
return Json(new JsonResultData() { Success = true, Message = "導(dǎo)入數(shù)據(jù)成功!" });
}
else
{
return Json(new JsonResultData() { Success = false, Message = "找不到導(dǎo)入文件數(shù)據(jù)!" });
}
}
catch (Exception ex)
{
return Json(new JsonResultData() { Success = false, Message = "導(dǎo)入數(shù)據(jù)失敗!" });
}
}
public static List<string[]> InportData(Stream filestream)
{
lock (RunningInport)
{
List<string[]> lstData = new List<string[]>();
string strLine = "";
bool IsFirst = true;
StreamReader sr = new StreamReader(filestream, Encoding.UTF8);
while ((strLine = sr.ReadLine()) != null)
{
if (IsFirst)
{
string[] strTitles = strLine.Split(',');
lstData.Add(strTitles);
}
else
{
string[] strData = strLine.Split(',');
lstData.Add(strData);
}
}
return lstData;
}
}到此這篇關(guān)于ASP.NET WebAPI導(dǎo)入CSV的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET中Validation驗(yàn)證控件正則表達(dá)式特殊符號(hào)的說明
本文介紹asp.net中RegularExpressionValidator控件中的幾種特殊字符串使用規(guī)則,并做了代碼演示,希望對(duì)大家有所幫助。2016-04-04
ASP.NET Core奇淫技巧之動(dòng)態(tài)WebApi的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core奇淫技巧之動(dòng)態(tài)WebApi的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
.NET Core使用FluentEmail發(fā)送郵件的示例代碼
這篇文章主要介紹了.NET Core使用FluentEmail發(fā)送郵件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
ASP.NET MVC4中使用Html.DropDownListFor的方法示例
這篇文章主要介紹了ASP.NET MVC4中使用Html.DropDownListFor的方法,結(jié)合實(shí)例形式分析了控制器數(shù)據(jù)源及Html.DropDownListFor顯示操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08
asp.net core新特性之TagHelper標(biāo)簽助手
這篇文章主要為大家詳細(xì)介紹了asp.net core新特性之TagHelper標(biāo)簽助手的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
asp.net(vb.net)獲取真實(shí)IP的函數(shù)
asp.net(vb.net)獲取真實(shí)IP的函數(shù),需要的朋友可以參考下。2010-11-11
完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁
這篇文章主要介紹了完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁,需要的朋友可以參考下2014-12-12

