圖片上傳_及時顯示

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   for   sp   

   其實到現在我從剛畢業的雄心壯志,已經變為攀爬的小鳥了.雖然在沒有任務的情況下貪玩著,不過在接到任務的時,我還是抱著百分之百的心去完成.

    在學校時,拖控制項是我最喜歡做的事,因為很簡單方便.從來沒有考略過效能之類的問題,絕對做出來我就很厲害了.但是現在到了公司完全是老鳥鄙視的對象,雖然公司沒有明確規定你不能拖控制項,但是可能是虛榮心吧,努力扔掉控制項,像js 進發. 我覺得我現在的狀態就是,出生的嬰兒,見一個學一個.

     今天就總結一個我自己知道的圖片上傳.

      js:

 1    $(function () { 2             $(‘#hidIFrame‘).load(function () { 3                 var retContent = $(‘#hidIFrame‘).get(0).contentWindow.document.body.innerText; 4                 if (retContent == 0) { 5                     alert(‘檔案不能上傳超過10M‘); 6                     return false; 7                 } 8                 else { 9 10                     $("#img").attr(‘src‘, ‘../img/‘ + retContent)11                 }12             });13             $("#fil_img").change(function () {14                 var img = $("#fil_img").val();15                 if (img == "") {16                     alert("請上傳圖片");17                     return false;18                 }19                 else {20                     if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(img)) {21                         alert("圖片類型必須是.gif,jpeg,jpg,png中的一種")22                         return false;23                     }24                 }25                 $("#form1").submit();26 27             });28         });

  頁面:

 <form id="form1" method="post" action="../Handel/H_img.ashx" enctype="multipart/form-data" target="hidIFrame">        <div>            <input type="file" id="fil_img" name="fil_img" />        </div>        <div>            <img id="img" src="" />        </div>    </form>    <iframe name="hidIFrame" id="hidIFrame" width="0" height="0" style="border: 0;"></iframe>
View Code

  其實要做到在當頁面顯示最關鍵的就是iframe,在這過程中一定要在 form中 設定一個跟iframe 同名的target值.

   在一般處理常式中存值:

      private void addImg(HttpContext context)        {            string retVal = "";            HttpPostedFile file = context.Request.Files["fil_img"];            string _extensionName = ioFile.Path.GetExtension(file.FileName).TrimStart(‘.‘);            //檔案不可以超過10M            if (context.Request.Files["fil_img"].ContentLength > 1024 * 1204 * 10)            {                retVal = "0";            }            else            {                //重新命名                retVal = Guid.NewGuid().ToString("N") + "_" + ioFile.Path.GetExtension(context.Request.Files["fil_img"].FileName);                file.SaveAs(ioFile.Path.Combine(HttpContext.Current.Server.MapPath("/img/"),                  retVal));            }            context.Response.Write(retVal);        }
View Code

 

 

          

 

圖片上傳_及時顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.