C# layui文件(excel)上传 保存附件 读取文件内容,进行数据库更新
项目场景:layui文件(excel)上传 保存附件 读取文件内容,进行数据库更新分析:提示:前端js:layui.use('upload', function () {var upload = layui.upload;//执行实例var uploadInst = upload.render({elem: '#UploadChange' //绑定元素, accept: 'file'.
·
项目场景:
layui文件(excel)上传 保存附件 读取文件内容,进行数据库更新
分析:
提示:前端js:
layui.use('upload', function () {
var upload = layui.upload;
//执行实例
var uploadInst = upload.render({
elem: '#UploadChange' //绑定元素
, accept: 'file'
, url: "/SampleRepository/UploadChange" //上传接口
, data: {
}
, before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
layer.load(); //上传loading
}
, done: function (data) {
//上传完毕回调
alert(data.res);
ReloadTable();
layer.closeAll('loading'); //关闭loading
}
, error: function (error) {
//请求异常回调
alert(error.res);
layer.closeAll('loading'); //关闭loading
}
});
uploadInst.reload({
accept: 'file'
});
});
提示:后端C#代码
public ActionResult UploadChange()
{
var file = Request.Files[0];
try
{
string fileName = file.FileName;
string name = DateTime.Now.ToString("yyyyMMdd_HHmmss_") + fileName;
string filePath = HttpContext.Server.MapPath("~/File/信息导入/") + name;
byte[] buffer;
var inputStream = file.InputStream;
int readLength = Convert.ToInt32(inputStream.Length);
buffer = new byte[readLength];
inputStream.Seek(0, SeekOrigin.Begin);
inputStream.Read(buffer, 0, readLength);
using (var outputSteam = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
outputSteam.Seek(0, SeekOrigin.Begin);
outputSteam.Write(buffer, 0, buffer.Length);
}
NPIOUser aaa = new NPIOUser();
DataSet srcDS = new DataSet();
srcDS = aaa.NPIOExcelToDS(filePath, "汇总表");string ExNumber = "";
int ExCount = 0;
for (int i = 0; i < srcDS.Tables[0].Rows.Count; i++)
{
string SampleNumber = "";
string SampleState = "";
string ChangeNumber = "";
DateTime OutDate = new DateTime();
SampleNumber = srcDS.Tables[0].Rows[i]["编号"].ToString();
SampleState = srcDS.Tables[0].Rows[i]["状态"].ToString();
ChangeNumber = srcDS.Tables[0].Rows[i]["单号"].ToString();
OutDate = Convert.ToDateTime(srcDS.Tables[0].Rows[i]["日期"]);
RepositorySampleData data = SampleRepository.RepositorySampleDatas.FirstOrDefault(p => p.编号 == SampleNumber);
if (data != null)
{
if (SampleState.Trim()!="")
{
data.状态 = SampleState;
}
if (ChangeNumber.Trim() != "")
{
data.单号 = ChangeNumber;
}
if (OutDate != null)
{
data.时间 = OutDate;
}
SampleRepository.UpdateSample(data);
}
else
{
ExCount++;
ExNumber += SampleNumber + ";";
}}
if (ExNumber.Length == 0)
{
return Content(JsonConvert.SerializeObject(new { res = "数据导入成功。无异常数据" }));
}
else
{
return Content(JsonConvert.SerializeObject(new { res = "数据导入成功。存在" + ExCount + "条异常数据未导入,机身编号为:" + ExNumber }));
}
}
catch (Exception error) { return Content(JsonConvert.SerializeObject(new { res = "出现异常情况,数据不完整,请联系开发人员。" })); }
}
解决方案:
提示:layui文件(excel)上传 保存附件 读取文件内容,进行数据库更新
public void UpdateSample(RepositorySampleData Samples)
{
SampleDataContext.Entry<RepositorySampleData>(Samples).State = EntityState.Modified;
try
{
SampleDataContext.SaveChanges();
}
catch (Exception error)
{
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)