asp.net儲存圖片

xbackzy發表於2018-06-13
HttpPostedFileBase logo = Request.Files["Imglogo"]; //從前臺獲取圖片屬性
if (logo.ContentLength != 0)
{
    string name = logo.FileName;
    //獲取字尾名  ToLower轉換成小寫
    string logojpg = Path.GetExtension(name).ToLower();
    if (logojpg != ".jpg" && logojpg != ".png" && logo.ContentLength>0)
    {
            return Json(new { state = false });
    }
    //獲取路徑
    string path = Server.MapPath("../imgs/");
    //生成新的檔名
    string guid = System.Guid.NewGuid().ToString();
    string newName = guid + logojpg; //生成新檔名
    //儲存檔案
    logo.SaveAs(path + newName);

}

相關文章