Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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
C# 如何在FileUpload控件Asp.net中重命名文件?_C#_Asp.net - Fatal编程技术网

C# 如何在FileUpload控件Asp.net中重命名文件?

C# 如何在FileUpload控件Asp.net中重命名文件?,c#,asp.net,C#,Asp.net,我有一个方法可以携带上传的图像,我需要在我的应用程序中将其重命名为固定名称,但当使用File.Copy方法时,它会返回结果(文件不存在) 我不知道这是怎么回事 我尝试了这个文件.Copy(UploadFile.FileName,newFilName); 也没有反应 string filename = Path.GetFileName(FileUpload.FileName); string extension = Path.GetExtension(FileUp

我有一个方法可以携带上传的图像,我需要在我的应用程序中将其重命名为固定名称,但当使用File.Copy方法时,它会返回结果(文件不存在) 我不知道这是怎么回事

我尝试了这个文件.Copy(UploadFile.FileName,newFilName); 也没有反应

 string filename = Path.GetFileName(FileUpload.FileName);
                string extension = Path.GetExtension(FileUpload.PostedFile.FileName);

                string oldFileName = FileUpload.FileName;
                string newFileName = ("aboutusImage" + extension);

                File.Copy(oldFileName, newFileName);
                File.Delete(oldFileName);

                File.Move(FileUpload.FileName, newFileName);
                FileUpload.SaveAs(Server.MapPath("~/Styles/aboutusImages/") + newFileName);

                var updateAboutus = (from a in dbContext.DynamicText
                                     where a.Id == 1
                                     select a).Single();
                updateAboutus.Id = updateAboutus.Id;
                updateAboutus.Name = updateAboutus.Name;
                updateAboutus.Image = Path.Combine("~/Styles/aboutusImages/", "aboutusImage.jpg");

                dbContext.SaveChanges();

FileUpload.FileName属性表示客户端计算机上的文件名,而不是服务器上的文件名。 除非当前文件夹中您自己的服务器上有相同的文件,否则尝试在其上进行文件复制成功的可能性很小


当然,下面的File.Delete、File.Move也有同样的问题,应该删除它们

您尝试从中移动的名称只是客户端提供的名称,而不是服务器上的当前名称。它可能位于服务器上的某个临时位置

你可能想要这个函数

protected void UploadButton_Click(object sender, EventArgs e)
{
    // Specify the path on the server to
    // save the uploaded file to.
    String savePath = @"c:\temp\uploads\";

    // Before attempting to perform operations
    // on the file, verify that the FileUpload 
    // control contains a file.
    if (FileUpload1.HasFile)
    {

      String fileName = FileUpload1.FileName;

      // Append the name of the file to upload to the path.
      savePath += fileName;


      // Call the SaveAs method to save the 
      // uploaded file to the specified path.
      // This example does not perform all
      // the necessary error checking.               
      // If a file with the same name
      // already exists in the specified path,  
      // the uploaded file overwrites it.
      FileUpload1.SaveAs(savePath);