System.IO.FileNotFoundException:找不到文件
System.IO.FileNotFoundException : Could not find file
我正在使用文件上传控件。但是当我尝试读取上传的文件时,它正在寻找创建项目的文件夹并给出错误。
这个
的代码
<input type="file" name="file" />
<button type="submit">Upload File</button>
和
[HttpPost]
public IActionResult UploadFile(IFormFile file)
{
string FileName = file.FileName;
if (file != null && file.Length != 0)
{
FileStream fileStream = new FileStream(FileName, FileMode.Open);
using (StreamReader streamReader = new StreamReader(fileStream))
{
string line = streamReader.ReadLine();
}
}
}
在表单操作中使用 enctype = "multipart/form-data"
。你可以用剃须刀@using (Html.BeginForm())
@using (Html.BeginForm("UploadFile", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<button type="submit">Submit</button>
}
源代码查看示例here
在你的控制器中,你可以使用这样的方法控制器:
public async Task<IActionResult> UploadFile(IFormFile file)
{
var uploadPath = Path.Combine(hostingEnv.WebRootPath, "uploadsfolder");
using (var fileStream = new FileStream(Path.Combine(uploadPath, file.FileName), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
return RedirectToAction("Index");
}
源代码控制器示例here
这应该可以正常工作
如果您尝试使用流读取上传的文件,您可以使用以下方法,
string result;
if (file != null && file.Length != 0)
{
using (var reader = new StreamReader(file.OpenReadStream()))
{
result = reader.ReadToEnd();
}
}
或者,如果您试图将上传的文件保存在服务器中的某个位置,那么您应该使用 CopyTo 方法,如下例所示,
var destinationPath= Path.GetTempFileName(); //Change this line to point to your actual destination
using (var stream = new FileStream(destinationPath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
我正在使用文件上传控件。但是当我尝试读取上传的文件时,它正在寻找创建项目的文件夹并给出错误。 这个
的代码 <input type="file" name="file" />
<button type="submit">Upload File</button>
和
[HttpPost]
public IActionResult UploadFile(IFormFile file)
{
string FileName = file.FileName;
if (file != null && file.Length != 0)
{
FileStream fileStream = new FileStream(FileName, FileMode.Open);
using (StreamReader streamReader = new StreamReader(fileStream))
{
string line = streamReader.ReadLine();
}
}
}
在表单操作中使用 enctype = "multipart/form-data"
。你可以用剃须刀@using (Html.BeginForm())
@using (Html.BeginForm("UploadFile", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<button type="submit">Submit</button>
}
源代码查看示例here
在你的控制器中,你可以使用这样的方法控制器:
public async Task<IActionResult> UploadFile(IFormFile file)
{
var uploadPath = Path.Combine(hostingEnv.WebRootPath, "uploadsfolder");
using (var fileStream = new FileStream(Path.Combine(uploadPath, file.FileName), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
return RedirectToAction("Index");
}
源代码控制器示例here
这应该可以正常工作
如果您尝试使用流读取上传的文件,您可以使用以下方法,
string result;
if (file != null && file.Length != 0)
{
using (var reader = new StreamReader(file.OpenReadStream()))
{
result = reader.ReadToEnd();
}
}
或者,如果您试图将上传的文件保存在服务器中的某个位置,那么您应该使用 CopyTo 方法,如下例所示,
var destinationPath= Path.GetTempFileName(); //Change this line to point to your actual destination
using (var stream = new FileStream(destinationPath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}