/// <summary>
/// 刪除文件
/// </summary>
/// <param name="saveFilePath">文件保存路徑</param>
/// <param name="imgPath">文件名</param>
public static void Delfile(string saveFilePath, string imgPath)
{
//刪除原有圖片
string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
if (File.Exists(pathname))
{
try
{
File.Delete(pathname);
}
catch (Exception error)
{
buss.ShowMsgBox("刪除原有圖片出錯! 錯誤:" + error.Message);
}
}
}
/// <summary>
/// 上傳圖片,返回要保存的圖片名
/// </summary>
/// <param name="fulPhoto">上傳控件名稱</param>
/// <param name="fileName">保存文件的文件夾路徑</param>
/// <param name="imgPath">要判斷是否存在的圖片名</param>
/// <returns>返回保存的圖片名</returns>
public static string Upimgfile(FileUpload fulPhoto, string saveFilePath, string imgPath)
{
if (fulPhoto.HasFile)
{
try
{
//刪除原有的
if (imgPath != "")
{
//刪除原有圖片
string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
if (File.Exists(pathname))
{
try
{
File.Delete(pathname);
}
catch (Exception error)
{
buss.ShowMsgBox("刪除原有圖片出錯! 錯誤:" + error.Message);
return "";
}
}
}
//如果有文件
string fileName = fulPhoto.FileName;
string savePath = System.Web.HttpContext.Current.Server.MapPath(saveFilePath);
string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);
//命名圖片
string imgName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + fileName;
//圖片保存路徑
string fullpath = savePath + "\\" + imgName;
//保存圖片
fulPhoto.SaveAs(fullpath);
imgPath = imgName;
}
catch (Exception ex)
{
buss.ShowMsgBox("圖片上傳出錯!錯誤:" + ex.Message);
return "";
}
}
return imgPath;
}
/// <summary>
/// 上傳圖片,返回要保存的圖片名
/// </summary>
/// <param name="fulPhoto">上傳控件名稱</param>
/// <param name="fileName">保存文件的文件夾路徑</param>
/// <param name="imgPath">要判斷是否存在的圖片名</param>
/// <returns>返回保存的圖片名</returns>
public static string Upimgfile(FileUpload fulPhoto, string saveFilePath, string imgPath,string aa)
{
if (fulPhoto.HasFile)
{
try
{
//刪除原有的
if (imgPath != "")
{
//刪除原有圖片
string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
if (File.Exists(pathname))
{
try
{
File.Delete(pathname);
}
catch (Exception error)
{
buss.ShowMsgBox("刪除原有圖片出錯! 錯誤:" + error.Message);
return "";
}
}
}
//如果有文件
string fileName = fulPhoto.FileName;
string savePath = System.Web.HttpContext.Current.Server.MapPath(saveFilePath);
string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);
//命名圖片
string imgName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + aa + fileName;
//圖片保存路徑
string fullpath = savePath + "\\" + imgName;
//保存圖片
fulPhoto.SaveAs(fullpath);
imgPath = imgName;
}
catch (Exception ex)
{
buss.ShowMsgBox("圖片上傳出錯!錯誤:" + ex.Message);
return "";
}
}
return imgPath;
}
/// <summary>
/// 上傳圖片,返回要保存的圖片名
/// </summary>
/// <param name="fulPhoto">上傳控件名稱</param>
/// <param name="fileName">保存文件的文件夾路徑</param>
/// <param name="imgPath">要判斷是否存在的圖片名</param>
/// <returns>返回保存的圖片名</returns>
public static string Upimgfile2(FileUpload fulPhoto, string saveFilePath, string imgPath,string numb)
{
if (fulPhoto.HasFile)
{
try
{
//刪除原有的
if (imgPath != "")
{
//刪除原有圖片
string pathname = System.Web.HttpContext.Current.Server.MapPath(saveFilePath) + "/" + imgPath;
string fname = pathname.Substring(pathname.LastIndexOf("\\") + 1);
if (File.Exists(pathname))
{
try
{
File.Delete(pathname);
}
catch (Exception error)
{
buss.ShowMsgBox("刪除原有圖片出錯! 錯誤:" + error.Message);
return "";
}
}
}
//如果有文件
string fileName = fulPhoto.FileName;
string savePath = System.Web.HttpContext.Current.Server.MapPath(saveFilePath);
string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);
//命名圖片
string imgName = System.DateTime.Now.ToString("yyyyMMddhhmmss")+ numb + fileName;
//圖片保存路徑
string fullpath = savePath + "\\" + imgName;
//保存圖片
fulPhoto.SaveAs(fullpath);
imgPath = imgName;
}
catch (Exception ex)
{
buss.ShowMsgBox("圖片上傳出錯!錯誤:" + ex.Message);
return "";
}
}
return imgPath;
}
C#上傳圖片返回要保存的圖片名
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.