Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Model view controller 在path.getFileName之后更改文件名_Model View Controller_.net - Fatal编程技术网

Model view controller 在path.getFileName之后更改文件名

Model view controller 在path.getFileName之后更改文件名,model-view-controller,.net,Model View Controller,.net,要上载图像,我使用以下代码: [HttpPost] public ActionResult Index(FotoModel model) { if (ModelState.IsValid) { if (model != null && model.File != null) { var fileName = Path.GetFileName(model

要上载图像,我使用以下代码:

[HttpPost]
    public ActionResult Index(FotoModel model)
    {

        if (ModelState.IsValid)
        {
            if (model != null && model.File != null)
            {
                var fileName = Path.GetFileName(model.File.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
                model.File.SaveAs(path);
                return View();
            }
        }

        return View();
    }
工作正常,但是否有更改文件名的选项?。我试着这样做:

fileName = FileMode(fileName, "user");

但是有一个例外:“System.IO.FileMode是一种类型,但像变量一样使用”

Path.Combine()允许将字符串组合到文件名中吗?所以你不能简单地做:

var fileName = "user";
var path = Path.Combine(Server.MapPath("~/Content/Images"), fileName);

在这种情况下,请使用以下命令删除扩展名,然后将其重新添加到新文件名中:

string newName = "user";
fileName = newName + fileName.Substring(fileName.LastIndexOf("."));

不,因为文件正在保存,没有任何扩展名。