检查Stream inputStream文件是否存在

时间:2013-07-19 15:08:46

标签: c# asp.net-mvc

我正在更改一个方法,用于接受临时文件夹的字符串和文件的字符串,并将其更改为流,我想要一些帮助,如何检查文件是否存在。

bool UploadFile(Stream inputStream, Stream inputFile);

这是我原来的,我想改变,所以参数接受一个流

bool UploadFile(string tempFolder, string fileName)

public bool UploadFile(string tempFolder, string fileName)
        {
            if (File.Exists(fileName))
            {
                testingUsage.Upload(tempFolder, fileName);  
                return testingUsage.Exists(tempFolder);
            }

            return false;
        }

为文件创建两个流,为位置创建一个流吗?

1 个答案:

答案 0 :(得分:1)

假设这是您的上传操作:

[HttpPost]
public ActionResult Upload()
{
    try
    {
        if (Request.Files.Count > 0)
        {
            string tempFolder = "...";
            var file = Request.Files[0];

            if(UploadFile(tempFolder, file))
            {
              // Return a View to show a message that file was successfully uploaded...
              return View();
            }   
        }
    }
    catch (Exception e)
    {
        // Handle the exception here...
    }
}

你的方法可以是这样的:

private bool UploadFile(string tempFolder, HttpPostedFileBase file)
{
   var path = Path.Combine(tempFolder, file.FileName);

   // if the file does not exist, save it.
   if (!File.Exists(path))
   {
      file.SaveAs(path);
      return true;
   }

   return false;
}