【发布时间】:2018-07-18 13:42:57
【问题描述】:
我使用 MVC 创建了文件上传表单。它可以上传pdf、图片、txt等文件,但不能上传微软word文件。列数据类型是 varbinary(Max)。 控制器代码:
public ActionResult Create(HttpPostedFileBase postedFile)
{
byte[] bytes;
using (BinaryReader br = new BinaryReader(postedFile.InputStream))
{
bytes = br.ReadBytes(postedFile.ContentLength);
}
string constr = WebConfigurationManager.ConnectionStrings["MyDataModel"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO mytable (filedata,mimetype) VALUES (@filepath, @filedata,@mimetype)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@filepath", Path.GetFileName(postedFile.FileName));
cmd.Parameters.AddWithValue("@mimetype", postedFile.ContentType);
cmd.Parameters.AddWithValue("@filedata", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
return View();
}
查看:
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
{
<input type="file" name="postedFile" />
<input type="submit" id="btnUpload" value="Upload" />
}
}
问题出在哪里?谢谢。
【问题讨论】:
-
您使用的是什么类型的服务器? IIS?
-
上传的错误是什么?
-
问题出在哪里?你告诉我们。你有错误吗?会发生什么?
-
请注意,model-view-controller 标签是针对有关模式的问题。 ASP.NET-MVC 实现有一个特定的标记。
标签: asp.net-mvc