我正在尝试将图像路径上传到数据库并将文件保存在图像文件夹中。除了 getExtension 部分(.jpg、.gif 等...)之外,此方法正常工作,Path.GetExtension 方法始终返回 null (""),我不知道为什么。
您可以在下面找到使用的代码:
string imagesFolderPath = Server.MapPath("//Images");
imagesFolderPath += "//";
string newImageName = addProduct.id.ToString();
string extension = Path.GetExtension(fileUpload.FileName.ToString());
addProduct.imagePath = "Images//" + newImageName + extension;
fileUpload.SaveAs(imagesFolderPath + newImageName);
最佳答案
根据 MSDN Documentation :
The extension of the specified path (including the period "."), or null, or String.Empty. If path is null, GetExtension returns null. If path does not have extension information, GetExtension returns String.Empty.
获得空字符串 ""
的唯一方法是not 的路径为 null
和到不的路径有扩展名。
你得到一个空字符串,因为路径没有扩展名。
关于c# - Path.GetExtension 返回 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14054483/