Shuan-han Chen Shuan-han Chen - 2 months ago 12
C# Question

Mvc C# upload pdf ,have error message (HTTP 404),but file are already upload to the designated path

I want to wirte Mvc C# upload pdf,but have some difficulties.
When I upload file,have error message (HTTP 404),
but file are already upload to the designated path.
How can I solve it ? thanks.

error message:


  • Server Error in '/' occurs app.

  • Resource not found.Description: HTTP 404. The resource you are
    looking for (or one of dependence) may have removed the name has
    changed, or is temporarily unavailable.

  • Please review the following URL, and make sure it's spelled
    correctly. URL requested: / MakeUpExam / Upload



code:

controller:

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");
}
}}


View



@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>





Models

public class testModel
{
public string a{ get; set; }
public string b{ get; set; }
public string c{ get; set; }
}

Answer

After your post to Upload, you try to redirect to Upload (return RedirectToAction("Upload");).

When we look at your controller, you have no HttpGet Action that is called Upload. You probably want to redirect somewhere else after the upload.