[解决立刻结贴]MVC jquery ajax上传 一个input file 点击按钮如果jquery ajax post到action中需要二进制插入数据库(无类型限制)非插件取的时候转换响应文件MVC中。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 C#public ViewResult Upload() { HttpPostedFileBase file = Request.Files["f1"]; if (file.ContentLength > 0) { file.SaveAs(Server.MapPath("~/") + System.IO.Path.GetFileName(file.FileName)); } //存入数据库 if (file.ContentLength > 0) { //得到文件数组 byte[] fileData = new Byte[file.InputStream.Length]; System.IO.MemoryStream ms = new System.IO.MemoryStream(fileData); ms.Read(fileData, 0, (int)ms.Length); ms.Close(); //得到文件大小 int fileLength = file.ContentLength; //得到文件名字 string fileName = System.IO.Path.GetFileName(file.FileName); //得到文件类型 string fileType = file.ContentType; //构建数据库连接,SQL语句,创建参数 string strCnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Image2Access.mdb"; System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strCnString); String strSql = "INSERT INTO Person (PersonName,PersonImageType,PersonImageFileName,PersonImageSize,PersonImage)" + "VALUES (@PersonName,@PersonImageType,@PersonImageFileName,@PersonImageSize,@PersonImage)"; System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(strSql, myConnection); command.Parameters.AddWithValue("@PersonName", "xx"); command.Parameters.AddWithValue("@PersonImageType", fileType); command.Parameters.AddWithValue("@PersonImageFileName", fileName); command.Parameters.AddWithValue("@PersonImageSize", fileLength); command.Parameters.AddWithValue("@PersonImage", fileData); //打开连接,执行查询 myConnection.Open(); command.ExecuteNonQuery(); myConnection.Close(); } ViewBag.Greeting = "上传成功"; return View(); }<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title></head><body> <form action="/Home/Upload" enctype="multipart/form-data" method="post"> <input type="file" name="f1"/> <input type="submit" value="upload" /> </form></body></html> LZ是不是想要这个效果http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html 这个靠谱我试下MVC 后台转二进制 然后插入oracle blob操作blob有什么需要注意的吗? 有些细节需要注意这个地方//得到文件数组 byte[] fileData = new Byte[file.ContentLength]; file.InputStream.Position = 0; //此句很关键 file.InputStream.Read(fileData, 0, file.ContentLength); //得到文件大小 int fileLength = file.ContentLength; //得到文件名字 string fileName = System.IO.Path.GetFileName(file.FileName);参见http://dotnet.aspx.cc/file/Upload-Multi-Files-In-ASP.NET-MVC3.aspx Asp.Net如何实现邮件订阅? Excel数据导入ORACLE ajax中get问题 为何VB开发的程序打包为SETUP后在另台机上安装后执行出现如下错误? 如何把文本框里的数据读出来转化成decimal类型存到数据库? 我想用javascript控制一个表格的某一列隐藏或显示??如何弄啊?? 服务器端include指令语法问题 JAVA和ASP我该学哪个,救救我吧前辈们!!~~ 鼠标移到网页的一个地方时自动显示文本。。。怎么做啊 亿万火及 怎样在repeater循环绑定的数据里不显示一条数据里的一个字段 AD用户怎么查找自己有哪些权限?
public ViewResult Upload()
{
HttpPostedFileBase file = Request.Files["f1"];
if (file.ContentLength > 0)
{
file.SaveAs(Server.MapPath("~/") + System.IO.Path.GetFileName(file.FileName));
}
//存入数据库
if (file.ContentLength > 0)
{
//得到文件数组
byte[] fileData = new Byte[file.InputStream.Length];
System.IO.MemoryStream ms = new System.IO.MemoryStream(fileData);
ms.Read(fileData, 0, (int)ms.Length);
ms.Close(); //得到文件大小
int fileLength = file.ContentLength;
//得到文件名字
string fileName = System.IO.Path.GetFileName(file.FileName); //得到文件类型
string fileType = file.ContentType; //构建数据库连接,SQL语句,创建参数
string strCnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Image2Access.mdb";
System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strCnString);
String strSql = "INSERT INTO Person (PersonName,PersonImageType,PersonImageFileName,PersonImageSize,PersonImage)" +
"VALUES (@PersonName,@PersonImageType,@PersonImageFileName,@PersonImageSize,@PersonImage)";
System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(strSql, myConnection);
command.Parameters.AddWithValue("@PersonName", "xx");
command.Parameters.AddWithValue("@PersonImageType", fileType);
command.Parameters.AddWithValue("@PersonImageFileName", fileName);
command.Parameters.AddWithValue("@PersonImageSize", fileLength);
command.Parameters.AddWithValue("@PersonImage", fileData); //打开连接,执行查询
myConnection.Open();
command.ExecuteNonQuery();
myConnection.Close();
}
ViewBag.Greeting = "上传成功";
return View();
}<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="/Home/Upload" enctype="multipart/form-data" method="post">
<input type="file" name="f1"/>
<input type="submit" value="upload" />
</form>
</body>
</html>
http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html
我试下MVC 后台转二进制 然后插入oracle blob
操作blob有什么需要注意的吗?
这个地方
//得到文件数组
byte[] fileData = new Byte[file.ContentLength];
file.InputStream.Position = 0; //此句很关键
file.InputStream.Read(fileData, 0, file.ContentLength);
//得到文件大小
int fileLength = file.ContentLength;
//得到文件名字
string fileName = System.IO.Path.GetFileName(file.FileName);参见
http://dotnet.aspx.cc/file/Upload-Multi-Files-In-ASP.NET-MVC3.aspx