Mvc c#上传pdf,有错误消息(HTTP 404),但文件已经上传到指定的路径
本文关键字:路径 文件 有错误 pdf 上传 消息 Mvc HTTP | 更新日期: 2023-09-27 18:08:19
我想用c#写Mvc上传pdf,但是有一些困难。当我上传文件时,有错误消息(HTTP 404),但是文件已经上传到指定的路径。我怎么解它?谢谢。
错误信息:
- 在'/'中出现服务器错误。
- 资源未找到。描述:HTTP 404。你是一个资源查找(或一个依赖)可能已删除的名称有更改,或暂时不可用。
- 请查看以下URL,并确保拼写正确正确。URL请求:/MakeUpExam/Upload
控制器:
public ActionResult MakeUpExam()
{
List<TesrModel> TesrModel = new List<TesrModel>();
using (SqlConnection Coon = new SqlConnection(connStr))
{
Coon.Open();
SqlCommand commandExamInfo = new SqlCommand();
using (commandExamInfo = new SqlCommand(@"select * from aaa where bbb=@bbb", Coon)) commandExamInfo.Parameters.Add(new SqlParameter("@bbb", "123"));
{
commandExamInfo.CommandType = CommandType.Text;
SqlDataReader reader;
reader = commandExamInfo.ExecuteReader();
{
if (reader.FieldCount != 0)
{
while (reader.Read())
{
TesrModel.Add(
new TesrModel()
{
a = reader["a"].ToString(),
b = reader["b"].ToString(),
c = reader["c"].ToString(),
});
}
}
}
}
}
return View(TesrModel);
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
using (SqlConnection Coon = new SqlConnection(connStr))
{
SqlCommand command = new SqlCommand();
Coon.Open();
using (command = new SqlCommand(@"select ccc from ddd where eee=@eee and fff= @fff", Coon))
command.Parameters.Add(new SqlParameter("@eee", "aaa"));
command.Parameters.Add(new SqlParameter("@fff", "bbb"));
{
if (file != null)
{
if (file.ContentLength <= 1042880)
{
if (file.ContentType == "pdf" || file.ContentType== "application/pdf")
{
var fileName = Path.GetFileName(file.FileName);
var Newfilename = fileName + "_" + DateTime.Now.ToString();
var path = Path.Combine(@"D:'data", Newfilename);
file.SaveAs(path);
}
else
{
ViewBag.Message = "msut pdf file";
}
}
else
{
ViewBag.Message = "file too large";
}
}
else
{
ViewBag.Message = "please select file, thanks.";
}
}
}
return RedirectToAction("Upload");
}
}}
视图@model IEnumerable<MakeUpExam.Models.TesrModel>
<h2>Upload</h2>
<table class="table" border="1">
<tr>
<th>
@Html.DisplayNameFor(model => model.a)
</th>
<th>
@Html.DisplayNameFor(model => model.b)
</th>
<th>
@Html.DisplayNameFor(model => model.c)
</th>
<th>
upload file
</th>
<th>
upload time
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.a)
</td>
<td>
@Html.DisplayFor(modelItem => item.b)
</td>
<td>
@Html.DisplayFor(modelItem => item.c)
</td>
<td>
<form action="@Url.Action("Upload")" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /><input type="submit" value="upload file" />
</form>
</td>
<td>
</td>
</tr>
}
</table>
模型public class testModel
{
public string a{ get; set; }
public string b{ get; set; }
public string c{ get; set; }
}
您的帖子到Upload
后,您试图重定向到Upload
(return RedirectToAction("Upload");
)。
当我们看你的控制器时,你没有名为Upload
的HttpGet Action。您可能希望在上传后重定向到其他地方。