溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Asp.net mvc4用iframe實(shí)現(xiàn)異步上傳

發(fā)布時(shí)間:2020-04-07 15:57:05 來(lái)源:網(wǎng)絡(luò) 閱讀:1628 作者:桂素偉 欄目:編程語(yǔ)言

1.         Model層用一個(gè)TestModel(與上一篇博文的TestModel 相同)

2.         Controller層:
 public class TestController : Controller
{
//這是iframe頁(yè)面的action
 public ActionResult IframeView()
        {
            return View();
        }
        public ActionResult View2()
        {
            return View();
        }
        /// <summary>
        /// 提交方法
        /// </summary>
        /// <param name="tm">模型數(shù)據(jù)</param>
        /// <param name="file">上傳的文件對(duì)象,此處的參數(shù)名稱(chēng)要與View中的上傳標(biāo)簽名稱(chēng)相同</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult View2(TestModel tm, HttpPostedFileBase file)
        {
            if (file == null)
            {
                return Content("沒(méi)有文件!", "text/plain");
            }
            var fileName = Path.Combine(Request.MapPath("~/UploadFiles"), Path.GetFileName(file.FileName));
            try
            {
                file.SaveAs(fileName);
                tm.AttachmentPath = fileName;//得到全部model信息
                return Content("上傳成功!", "text/plain");
            }
            catch
            {
                return Content("上傳異常 !", "text/plain");
            }
        }
}
View層,現(xiàn)在有兩個(gè)View層,一個(gè)是iframe,叫IframeView,一個(gè)是View2,分別如下:
IframeView
@model UploadFile.Models.TestModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>IframeView</title>
</head>
<body>
    @*enctype= "multipart/form-data"是必需有的,否則action接收不到相應(yīng)的file,這里報(bào)交的action是View2*@
    @using (Html.BeginForm("View2", "Test", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.LabelFor(mod => mod.Title)
        <br />
        @Html.EditorFor(mod => mod.Title)
        <br />     <br />
        @Html.LabelFor(mod => mod.Content)
        <br />
        @Html.EditorFor(mod => mod.Content) 
        <br />   
        <span>上傳文件</span>
        <br />
        <input type="file" name="file" />
        <br />
        <br />
        <input id="ButtonUpload" type="submit" value="提交" />
    }
</body>
</html>
別一個(gè)是View2
@{
    Layout = null;
}
 
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>iframe異步上傳</title>
</head>
<body>
     <form>
        <iframe width="500" height="500" src="/Test/IframeView"></iframe>
    </form>
</body>

</html>

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI