c# - Path.GetExtension 返回 ""

标签 c# file-upload upload

我正在尝试将图像路径上传到数据库并将文件保存在图像文件夹中。除了 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/

相关文章:

PHP Curl 文件上传发送空上传

c# - 具有多个参数的三元语句

c# - 如何将一个 TreeView 实例的节点添加到同一 TreeView 的另一个实例

java.lang.ClassNotFoundException : com. oreilly.servlet.MultipartRequest

javascript - 如何设置文本框值以使用 javascript 上传文件名?

python - 在 Python 中上传文件内容

c# - 如何根据动态字段名引用 Linq 中的字段

c# - 当 HTTP 状态为错误请求时,HttpResponseMessage 内容丢失

c# - 文件上传控件仅上传PDF

javascript - knox S3 上传损坏或 chop 的文件