[英]Mvc C# upload pdf ,have error message (HTTP 404),but file are already upload to the designated path
我想写Mvc C#上传pdf,但是有一些困难。 当我上传文件时,有错误信息(HTTP 404),但文件已经上传到指定路径。 我该如何解决? 谢谢。
错误信息:
代码:
控制器:
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 。 您可能想在上传后重定向到其他地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.