1. 程式人生 > >asp.net-html圖片的上傳

asp.net-html圖片的上傳

校驗 消息 data date map error ext 提交 ring

1,文件的上傳存在一個非常致命的文件,要是上傳病毒文件,可能會使你的系統崩潰

所以判斷文件的類型的非常重要的。

不能單獨與後綴名判斷,下面展示一種webform類型的上傳文件案例。

1,書寫html語言

<html>
 <head></head>
 <body>
  <li class="l-fieldcontainer" fieldindex="14">
   <ul>
    <li style="width:120px;text-align:right;">*企業營業執照:</li>
    <
li id="mainform|14" style="width:100px;text-align:left;"> <div class="l-text" style="width: 98px;"> <input name="COMPANYLICENSE" validate="{&quot;maxlength&quot;:300,&quot;required&quot;:true}" class="l-text-field upper" style="width: 94px;" ligeruiid="COMPANYLICENSE"
id="COMPANYLICENSE" type="text" /> </div></li> <li><img src="#" heigth="50px" id="updatePic" width="60px" /></li> <li> <form id="EditMenuForm" enctype="multipart/form-data"> <input name="MenuPic" id="MenuPic" type="file" /> <
input name="btnUpload" id="btnUpload" value="上傳" style="width:60px" type="button" /> <label style="color:red">支持png,jpg格式圖片,大小不能超過2M,必須清晰可見</label> <label></label> </form></li> <li style="width:3px;"><span class="l-star">*</span></li> </ul></li> </body> </html>

技術分享圖片

書寫javasecript

ajaxSubmit的js文件來自:
  //上傳圖片
            $(‘#btnUpload‘).click(function () {
                if ($("#MenuPic").val()) {
                    //讓表單異步的提交到後臺。
                    $("#EditMenuForm").ajaxSubmit({
                        error: function (error) { alert(error); },
                        url: ‘handler/validate.ashx‘, /*設置post提交到的頁面*/
                        type: "post", /*設置表單以post方法提交*/
                        dataType: "text", /*設置返回值類型為文本*/
                        data: { "Action": "PIC" },
                        success: function (data) {
                            if (data == "-1") { alert("圖片大小不能超過2M"); return; }
                            if (data == "-2") { alert("圖片格式不正確,只支持png,jpg格式的圖片"); return;}
                            $("#MenuIcon").val(data);
                            $("#updatePic").attr("src", data);
                            liger.get("COMPANYLICENSE").setValue(data)
                            $("#MenuPic").val("");
                            $("#UpdateInfo").text("上傳成功!");
                        }
                    });
                } else {
                    $.messager.alert("錯誤消息", "請選擇合法圖片!");
                }
            })

書寫後臺管理系統

void ValidatePIC(HttpContext context)
    {
        //上傳圖片
        try
        {
            var requestFile = context.Request.Files["MenuPic"];
            string fname = "/lib/uploads/" + Guid.NewGuid().ToString() + System.IO.Path.GetExtension(requestFile.FileName);
            string path = context.Request.MapPath(fname);
            requestFile.SaveAs(path);
            #region 校驗圖片
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            if (fs.Length > 1024 * 1024*4) {
                 context.Response.Write("-1");
                 File.Delete(path); //刪除文件   
                 return;
            }
                BinaryReader reader = new BinaryReader(fs);
                string fileClass;
                byte buffer;
                byte[] b = new byte[2];
                buffer = reader.ReadByte();
                b[0] = buffer;
                fileClass = buffer.ToString();
                buffer = reader.ReadByte();
                b[1] = buffer;
                fileClass += buffer.ToString();
                reader.Close();
                fs.Close();
            #endregion
                if (fileClass == "255216" || fileClass == "7173" ||  fileClass == "13780")
                {
                    //255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,13780:png    
                    //Response.Write("圖片可用");     
                    //保存到數據庫中    
                    context.Response.Write(fname);
                }
                else
                {
                    context.Response.Write("-2");    
                    File.Delete(path); //刪除文件    
                    return;
                }
          
        }
        catch (Exception e)
        {
            context.Response.Write("-2");
            LogManager.WriteLog("上傳圖片出錯!" + e.Message);
        }
    }

asp.net-html圖片的上傳