2011-02-18 76 views
1
FileUpload img = (FileUpload)UploadImg; 
     Byte[] imgByte = null; 
     if (img.HasFile && img.PostedFile!=null) 
     { 
      HttpPostedFile File = UploadImg.PostedFile; 
      imgByte = new Byte[File.ContentLength]; 
      File.InputStream.Read(imgByte, 0, File.ContentLength); 
     } **strong text** 

在这里,我要保存的图像数据库。所以我想将图像转换为字节数组。为此我写了上面的代码。但这里的,如果不执行条件如何保存图像数据库

+0

陌生,您能不能告诉你的aspx? – 2011-02-18 10:30:38

+1

除非绝对重要的是图像存储在数据库中,否则我建议将它们作为普通文件存储在文件系统中,并将URI存储到数据库中的文件中。在数据库中存储的图像炸毁你的数据库和在大多数情况下并没有提供多少增值(我看到医疗影像它可能是有用的情况下)。以这种方式存储图像还可以让您将图像存储卸载到CDN。 – 2011-02-18 10:44:58

回答

0

假设你有你的页面上的文件上传控制:

<asp:FileUpload id="UploadImg" runat="server" /> 
<asp:Button id="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /> 

下面应该工作:

protected void UploadButton_Click(object sender, EventArgs e) 
{ 
    if(UploadImg.HasFile) 
    { 
     byte[] imgByte = UploadImg.FileBytes; 
     string filename = Path.GetFileName(UploadImg.FileName); 
     // TODO: Save the image to the database 
    } 
}