溫馨提示×

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

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

MVC 通過(guò)ajaxSubmit上傳圖片并顯示

發(fā)布時(shí)間:2020-06-26 03:00:15 來(lái)源:網(wǎng)絡(luò) 閱讀:1050 作者:8218729 欄目:編程語(yǔ)言
js代碼
function submitform() {
        $("#form_upload").ajaxSubmit({
            success: showResponse
        });
    }

    function showResponse(responseText) {
        if (responseText != null) {
            alert('上傳成功!');
        } else {
            alert('操作失敗!');
        }
    }

    $(function () {
        $("#upImg").on("change", function () {
            var file = this.files[0];

            if (this.files && file) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#result').attr('src', e.target.result);
                }
                reader.readAsDataURL(file);
            }
        });
    })
前臺(tái)代碼
<table>
                <tr>
                    <td >生產(chǎn)(經(jīng)營(yíng))許可證證件照片</td>
                    <td>
                        <form id="form_upload"  action="Upload" target="iframeInfo" method="post" enctype="multipart/form-data">
                            <input name="upImg" id="upImg" type="file" />
                            <input type="submit" value="上傳" />
                        </form>                        
                    </td>
                </tr>
                <tr>
                    <td>
                        <img id="result"  src="" alt="">
                    </td>
                    <td>
                        <iframe name="iframeInfo" id="iframeInfo" ></iframe>
                    </td>
                </tr>
            </table>
            
 (這里添加iframe,因?yàn)楹笈_(tái)返回時(shí)會(huì)跳轉(zhuǎn),把form放入iframe里提交就不會(huì)跳轉(zhuǎn)頁(yè)面)
后臺(tái)代碼 
[HttpPost]        
        public ActionResult Upload(HttpPostedFileBase upImg)
        {
            if (upImg == null)
            {
                return Content("文件上傳錯(cuò)誤,請(qǐng)重新選擇文件!");

            }
            string fileName = System.IO.Path.GetFileName(upImg.FileName);
            string filePhysicalPath = Server.MapPath("~/credp_w_picpaths/" + fileName);
            try
            {
                upImg.SaveAs(filePhysicalPath);
                Session["ImgPath"] = filePhysicalPath;
                return Content("上傳成功");
            }
            catch
            {
                return Content("上傳異常 !");

            }
        }


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

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎ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