2013-05-07 81 views
0

我需要上傳在網站中選擇的圖像到數據庫我的項目有多個文件選擇「文件上傳」如何將多個文件上傳到數據庫二進制格式的圖像數據,我的代碼是如何將選定的多個圖像上傳到數據庫

protected void Button1_Click(object sender, EventArgs e) 

    { 

     string Qry = "insert into tblFiles values(@data)"; 
     SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password"); 

     SqlCommand cmd = new SqlCommand(Qry,con); 
     cmd.Parameters.Add("@data") = FileUpload1.FileBytes; 

    } 

我使用下面的Web處理程序保存文件在本地文件夾

<%@ WebHandler Language="C#" Class="Upload" %> 

using System; 
using System.Web; 
using System.IO; 


public class Upload : IHttpHandler { 

public void ProcessRequest (HttpContext context) { 
    context.Response.ContentType = "text/plain"; 
    context.Response.Expires = -1; 
    try 
    { 
     HttpPostedFile postedFile = context.Request.Files["Filedata"]; 

     string savepath = ""; 
     string tempPath = ""; 
     tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"]; 
     savepath = context.Server.MapPath(tempPath); 
     string filename = postedFile.FileName; 
     if (!Directory.Exists(savepath)) 
      Directory.CreateDirectory(savepath); 

     postedFile.SaveAs(savepath + @"\" + filename); 
     context.Response.Write(tempPath + "/" + filename); 
     context.Response.StatusCode = 200; 
    } 
    catch (Exception ex) 
    { 
     context.Response.Write("Error: " + ex.Message); 
    } 
} 

public bool IsReusable { 
    get { 
     return false; 
    } 
} 

}

我用的是下面得離譜PT

<script type = "text/javascript"> 
     $(window).load(
function() { 
    $("#<%=FileUpload1.ClientID%>").fileUpload({ 
     'uploader': 'scripts/uploader.swf', 
     'cancelImg': 'images/cancel.png', 
     'buttonText': 'Browse Files', 
     'script': 'Upload.ashx', 
     'folder': 'Uploads', 
     'fileDesc': 'Image Files', 
     'fileExt': '*.jpg;*.jpeg;*.gif;*.png', 
     'multi': true, 
     'auto': false 
    }); 

,但我想將圖像存儲在數據庫


@ Damith

我有以下提到的代碼,但它沒有工作試過,

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string [email protected]"E:\Rajesh_Kumar\Application\Valuation\ExamManagement\ExamManagement\Uploads"; 
     string path = System.Configuration.ConfigurationManager.AppSettings[FolderPath]; 
     string Qry = "insert into tblFiles values(@data) Values (data)"; 
     SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=rajesh"; 
     StreamReader sr = new StreamReader(path); 
     while (sr.ReadLine() != null) 
     { 

       using (SqlCommand cmd = new SqlCommand(Qry, con)) 
       { 
        cmd.Parameters.Add("@data",SqlDbType.VarBinary).Value = path; 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
       } 
      con.Close(); 
      con.Dispose(); 
     } 

    } 
+1

你真的不應該暴露你的'sa'密碼。您的應用程序也不應以「sa」用戶身份登錄。 – 2013-05-07 10:59:39

+0

執行命令,你會被設置。你的問題是什麼? – CodeCaster 2013-05-07 11:07:28

+0

@CodeCaster問題是,目前只處理單個文件 – LukeHennerley 2013-05-07 11:10:27

回答

2

試用

foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles) 
{ 
    SaveImage(uploadedFile); 
} 

private void SaveImage(HttpPostedFile file) 
{ 
    using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString 
    { 
     using(SqlCommand cmd = new SqlCommand(Qry,con)) // set Qry 
     { 
      cmd.Parameters.AddWithValue("@data", ReadFile(file)); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
    } 
} 

private byte[] ReadFile(HttpPostedFile file) 
{ 
    byte[] data = new Byte[file.ContentLength]; 
    file.InputStream.Read(data, 0, file.ContentLength); 
    return data; 
} 

如果您需要從服務器文件夾中插入圖像,並假設你有成像路徑陣列imageArray然後

foreach (var path in imageArray) 
{ 
    SaveImage(path); 
} 

private void SaveImage(string path) 
{ 
    using(SqlConnection con = new SqlConnection(ConnectionString))// set ConnectionString 
    { 
     using(SqlCommand cmd = new SqlCommand(Qry,con)) // set Qry 
     { 
      cmd.Parameters.AddWithValue("@data", System.IO.File.ReadAllBytes(path)); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
    } 
} 
+0

@ Damith文件上傳1只發布文件沒有發佈文件我是否需要添加一些參考? – Rajesh 2013-05-08 09:17:46

+0

'.NET Framework 4.5'需要這個。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfiles.aspx – Damith 2013-05-08 09:24:16

+0

@ Damith我只有.Net Framework 4.0和我的操作系統是Windows XP所以它不支持.NET Framework 4.5有沒有其他方法可以像訪問「FileUpload1.PostedFiles」一樣引用 – Rajesh 2013-05-08 09:55:00

1
foreach(HttpPostedFile file in FileUpload1.PostedFiles) 
{ 
    var memoryStream = new MemoryStream(); 
    file.InputStream.CopyTo(memoryStream); 
    string Qry = "insert into tblFiles values(@data)"; 
     SqlConnection con = new SqlConnection(@"Data Source=ESLHPC17\SQL2008;Initial Catalog=dbFiles;User ID=user;Password=password"); 

     SqlCommand cmd = new SqlCommand(Qry,con); 
     cmd.Parameters.Add("@data") = memoryStream.ToArray(); 

} 
+1

查詢,命令和連接可以在'foreach'之外初始化,命令不會被執行。 – CodeCaster 2013-05-07 11:22:13