小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C#后臺(tái)識(shí)別圖片格式

 男人要敢擔(dān)當(dāng) 2017-05-24
public void Upload()
        {
            string imgurl = "";
            foreach (string key in Request.Files)
            {
                //這里只測(cè)試上傳第一張圖片file[0]
                HttpPostedFileBase file0 = Request.Files[key];

                //轉(zhuǎn)換成byte,讀取圖片MIME類型
                Stream stream;
                int size = file0.ContentLength / 1024; //文件大小KB

                if (size > 1024)
                {
                    //return Json("圖片不能超過(guò)1M",JsonRequestBehavior.DenyGet);
                }

                byte[] fileByte = new byte[2];//contentLength,這里我們只讀取文件長(zhǎng)度的前兩位用于判斷就好了,這樣速度比較快,剩下的也用不到。
                stream = file0.InputStream;
                stream.Read(fileByte, 0, 2);//contentLength,還是取前兩位

                //獲取圖片寬和高
                //System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                //int width = image.Width;
                //int height = image.Height;


                string fileFlag = "";
                if (fileByte != null || fileByte.Length == 0)//圖片數(shù)據(jù)是否為空
                {
                    fileFlag = fileByte[0].ToString()+fileByte[1].ToString();
                }
                string[] fileTypeStr = { "255216", "7173", "6677", "13780" };//對(duì)應(yīng)的圖片格式j(luò)pg,gif,bmp,png
                if (fileTypeStr.Contains(fileFlag))
                {
                    string action = Request["action"];
                    string path = "Views\\FLOW\\";
                    switch (action)
                    {
                        case "headimage":
                            path = "headimage/";
                            break;
                        case "blogtype":
                            path = "blogtype/";
                            break;
                    }
                    string fullpath = path;
                    if (!Directory.Exists(Server.MapPath(fullpath)))
                    {
                        Directory.CreateDirectory(Server.MapPath(fullpath));
                    }
                    
                    string map = "~/" + fullpath + Request.Files[0].FileName;
                    Request.Files[key].SaveAs(Server.MapPath(map));
                    imgurl = fullpath + Request.Files[key].FileName;
                }
                else
                {
                    //return Json("圖片格式不正確",JsonRequestBehavior.DenyGet);
                }

                stream.Close();
            }

            //return Json("上傳成功", JsonRequestBehavior.DenyGet);
        }





/*前臺(tái):from表單或者上傳組件直接訪問(wèn)該方法即可*/

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多