道理相通,我简单分享下在.net MVC下的实装。
1.制作Model类
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace RCRS.WebApp.LG.EM.Models{ //---------------------------------------------------------------- /// <summary> /// Import画面用 /// </summary> //---------------------------------------------------------------- public class tmp_UploadFile { /// <summary></summary> public HttpPostedFileBase FileName { get; set; } }}
2.实装controller里的对应方法,我这个处理逻辑比较复杂,懒得修改了,反正就这个意思
//---------------------------------------------------------------- /// <summary> /// アップロ ド /// </summary> /// <returns></returns> //---------------------------------------------------------------- [HttpPost] public virtual ActionResult UploadFile() { HttpPostedFileBase uploadedFile = Request.Files["FileName"]; string message = "アップロ ド失 しました。"; bool isUploaded = false; string path = ""; string dateTimeNow = DateTime.Now.ToString("yyMMdd-hhmmss"); string userName = User.Identity.GetUserName(); string uploadMsg = string.Empty; if (uploadedFile != null && uploadedFile.ContentLength != 0) { string pathForSaving = Server.MapPath("~/App_Data/Uploaded/"); try { if (BsnssBihin.IsExcel(uploadedFile.FileName)) { path = System.IO.Path.Combine(pathForSaving, dateTimeNow + "_" + uploadedFile.FileName); uploadedFile.SaveAs(path); isUploaded = BsnssBihin.UploadBihinChange(path, userName, ref uploadMsg); if (isUploaded) { message = "アップロ ド成功しました!" + "/n" + uploadMsg; Logger.Info("[成功] 品アップロ ド, " + dateTimeNow + ", " + "[" + userName + "]" + "[" + path + "]" + uploadMsg); } else { message = "アップロ ド失 しました。" + "/n" + uploadMsg; Logger.Info("[失 ] 品アップロ ド, " + dateTimeNow + ", " + "[" + userName + "]" + "["+path + "]" + uploadMsg); } } else { message = "ファイルの形式は不正です。"; } } catch (Exception ex) { message = string.Format("失 しました: {0}", ex.Message); Logger.Info("[失 ] 品アップロ ド: " + ex.Message + dateTimeNow + ", " + "[" + userName + "]" + "[" + path + "]"); } } return Json(new { isUploaded = isUploaded, message = message }, "text/html"); }
3.页面的实装
@model RCRS.WebApp.LG.EM.Models.tmp_UploadFile<table align="center" style="margin-bottom:200px"> <tr> <td> <div style="width:470px"> <input type="text" id="tbx-file-path" value="ファイルを してください" readonly="readonly" /> </div> </td> <td> <div style="width: 60px"> <span class="btn btn-primary fileinput-button"> <span>