close
當需要上傳圖片時,
為了怕使用者上傳成其他檔案格式造成程式錯誤
所以需要檢查
sample code 如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 檢查上傳檔案是否為圖片格式 | |
/// </summary> | |
/// <param name="fileName">完整檔案名稱</param> | |
/// <returns>True or False</returns> | |
private Boolean CheckPhotoFormat(String fileName) | |
{ | |
Boolean flag = false; | |
String fileExtension = Path.GetExtension(fileName).ToLower(); | |
String[] allowedExtensions = { ".jpg",".jpeg",".png",".gif"}; | |
for (int i = 0; i < allowedExtensions.Length; i++) | |
{ | |
if (allowedExtensions[i].ToString().Equals(fileExtension)) | |
{ | |
flag = true; | |
} | |
} | |
return flag; | |
} |
全站熱搜