??ν??????????????SQL Server???????
???????????? ???????[ 2013/9/12 13:23:15 ] ????????
?????????????????ν??????????????SQL Server??????С?
????????????У??????varbinary(MAX)?洢??
?????????????MVC3????????????????????????????
????View??
@{
ViewBag.Title = "UpLoadImg";
}
@using (Html.BeginForm("Create"?? "UpLoadImg"?? FormMethod.Post?? new { enctype = "multipart/form-data" }))
{
<h2>
UpLoadImg</h2>
<div id="mainform">
<div>
<input type="file" id="UpLoadFile" name="UpLoadFile" />
<input id="btnUpLoad" type="submit" value="???" />
</div>
<div>
</div>
</div>
}
????Controller???Action??
public ActionResult Create()
{
string filename = string.Empty;
string filetype=string.Empty;
byte[] filecontext=null;
HttpPostedFileBase filebase = Request.Files["UpLoadFile"];
if (filebase.ContentLength > 0)
{
Stream stream = filebase.InputStream;
byte[] by = new byte[filebase.ContentLength];
int i = stream.Read(by??0??filebase.ContentLength);
stream.Close();
string[] arrs = filebase.FileName.Split('\');
if (arrs.Length > 0)
{
filename = arrs[arrs.Length - 1];
}
else
{
filename = filebase.FileName;
}
filetype=filebase.ContentType;
filecontext=by;
}//?????????????????????????????????????????????????????????
int count = 0;
#region ????????
try
{
string ImageStore = System.Configuration.ConfigurationManager.AppSettings["ConnectionStrImageStore"].ToString().Trim();
string sqlStr = string.Empty;
sqlStr = @"INSERT INTO [Images] ([filename]??[filetype]??[filecontext]??[uploadtime])
VALUES(@filename??@filetype??@filecontext??@uploadtime)";
SqlConnection connection = new SqlConnection(ImageStore);
SqlCommand command = new SqlCommand(sqlStr?? connection);
command.Parameters.AddWithValue("@filename"??filename);
command.Parameters.AddWithValue("@filetype"??filetype);
command.Parameters.AddWithValue("@filecontext"??filecontext);
command.Parameters.AddWithValue("@uploadtime"??DateTime.Now);
command.Connection.Open();
count=command.ExecuteNonQuery();
command.Connection.Close();
}
catch
{
}
#endregion
if (count > 0)
{
return RedirectToAction("UpLoadImg");
}
else
{
return RedirectToAction("Index");
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11