一个input file 点击按钮如果jquery ajax post到action中需要二进制插入数据库(无类型限制)非插件取的时候转换响应文件MVC中。

解决方案 »

  1.   

    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>
      

  2.   

    LZ是不是想要这个效果
    http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html
      

  3.   

    这个靠谱
    我试下MVC 后台转二进制 然后插入oracle  blob
    操作blob有什么需要注意的吗?
      

  4.   

    有些细节需要注意
    这个地方
    //得到文件数组
          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