ASP.NET图片上传(配合jquery.from.js 插件)
前端:
js:
??????? function AjaxKouBeiShopEdit() {
??????????? var options = {
??????????????? dataType: "json",
??????????????? success: function (data) {
??????????????????? alert(data);
?????????????????
??????????????? },
??????????????? error: function (XMLHttpRequest, textStatus, errorThrown) {
??????????????????? console.log(textStatus);
??????????????????? console.log(errorThrown);
??????????????? }
??????????? };
???????? ?
??????????? $("#ShopForm").ajaxSubmit(options);
??????? }
?
html:
?<form class="form-horizontal" id="ShopForm" action="你的控制器/AjaxKouBeiShopEdit" method="post" enctype="multipart/form-data">
? <input type="file"? name="ProductImg" class="fileData"??? />
? <input type="file"? name="ProductImg" class="fileData"??? />
</form>
后端:
??????? /// <summary>
??????? /// 口碑門店-添加
??????? /// </summary>
??????? /// <param name="upImg"></param>
??????? /// <param name="entity"></param>
??????? /// <returns></returns>
??????? [HttpPost]
??????? public string AjaxKouBeiShopEdit(KouBeiShopModel entity)
??????? {
??????????? StringBuilder sqlStr = new StringBuilder();
??????????? TimeSpan timeSpan = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));
??????????? Random random = new Random();
??????????? JObject resultJsonerr = new JObject();
??????????? //修改操作
??????????????? #region? 添加文件
??????????????? string filePhysicalPath = @"D:/yyyy/";
??????????????? string MainImgName = "";
??????????????? //詳細(xì)圖片路徑
??????????????? if (Request.Files.Count != 0)
??????????????? {
??????????????????? MainImgName = filePhysicalPath + MainImgName;
??????????????????? try
??????????????????? {
??????????????????????? for (int i = 0; i < Request.Files.Count;i++ )
??????????????????????? {
??????????????????????????? HttpPostedFileBase uploadFile = Request.Files[i] as HttpPostedFileBase;
??????????????????????????? if (uploadFile != null && uploadFile.ContentLength > 0)
??????????????????????????? {
????????????????????????????? //? if (i==0)//首圖
???????????????????????????? // ? {
??????????????????????????????? //html 上至下加載,file? input
???????????????????????????? // ? }
//隨機(jī)名稱
??????????????????????????????? long t2 = (long)timeSpan.TotalSeconds;
??????????????????????????????? int n2 = random.Next(10000, 99999);
??????????????????????????????? string extension = Path.GetExtension(uploadFile.FileName);
??????????????????????????????? string ImgListName = t2 + "" + n2 + extension;
??????????????????????????????? var path = Path.Combine(filePhysicalPath, ImgListName);
??????????????????????????????? uploadFile.SaveAs(path);
??????????????????????????? }
??????????????????????? }
??????????????????? }
??????????????????? catch (Exception ex)
??????????????????? {
??????????????????????? resultJsonerr.Add("State", -1);
????????????????? ????? return "添加失敗";
??????????????????? }
??????????????? }
??????????? return "添加成功";
??????? }
?
2017-12-1? 提示:
HttpPostedFileBase是有序的,按照HTML加載方式來確定索引,從而可以控制首圖附屬圖等等。
?
轉(zhuǎn)載于:https://www.cnblogs.com/j2ee-web-01/p/7816002.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的ASP.NET图片上传(配合jquery.from.js 插件)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有点小绝望
- 下一篇: 深刻理解数据库外键含义