从文件获取原始文件名 [英] Get original filename from File
问题描述
我有一个需要System.File的动作
I have an action that takes in a System.File
public bool UploadToServer( File file )
我想在文件到达服务器后在服务器中使用文件的原始名称.我查看了 MSDN的文件类,但是看不到任何可以获取文件名或文件路径的东西.我可以从File
使用某个属性来获取其原始名称吗?还是应该简单地使签名看起来像这样:
And I would like to use the file's original name in the server once it gets there. I have looked in MSDN's File Class but don't see anything that looks like I could get the filename or for that matter, the filepath. Is there an attribute I can use from the File
to get it's original name or should I simply make the signiture look like this:
public bool UploadToServer( File file, string fileName )
?
正如@Marko所建议的那样,HttpPostedFile
是我所追求的,但我的资源中没有Server.Web
这个项目,而这正是让我绊倒的.
As @Marko suggested HttpPostedFile
is what I went with, I did not have Server.Web
in my resources for that project which was what was tripping me up.
推荐答案
尝试下面的代码,这样您就不必关心路径或任何其他安全问题.
Try the code below, this way you do not need to care about the path or any other security issues.
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
file.SaveAs(path);
}
}
这篇关于从文件获取原始文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!