C# Can';不能将图片保存在数据库中

C# Can';不能将图片保存在数据库中,c#,asp.net,sql,sql-server,C#,Asp.net,Sql,Sql Server,我正试图上传数据到我的数据库,包括图像。除了图像,其他一切都很好 我正在使用FileUpload组件: <asp:FileUpload runat="server" ID="ImgFileUpload" /> 我的SQL Server数据库:由于除了图片以外的所有内容都可以正常工作,因此我只需介绍表中的图像行即可: Column Name: Picture . Data Type: image . Allow Nulls: Allowed. 当我试图上传一张图片时,我在图

我正试图上传数据到我的数据库,包括图像。除了图像,其他一切都很好

我正在使用
FileUpload
组件:

<asp:FileUpload runat="server" ID="ImgFileUpload" />
我的SQL Server数据库:由于除了图片以外的所有内容都可以正常工作,因此我只需介绍表中的图像行即可:

Column Name: Picture .  Data Type: image  .   Allow Nulls: Allowed.
当我试图上传一张图片时,我在图片的表格中得到的是这个()


由于我无法上传图像,问题出在哪里?我强烈建议您将列数据类型更改为长文本

之后,将字节数组转换为base64字符串

String imageString = Convert.ToBase64String(pic);
或者,您可以在base64字符串之前添加“data:image/png;base64”,以便可以直接在html5中使用

imageString = "data:image/png;base64," + imageString;

您可能需要按照说明和重置寻道位置。您的代码可能如下所示:

int length = ImgFileUpload.PostedFile.ContentLength;
byte[] pic = new byte[length];
ImgFileUpload.PostedFile.InputStream.Seek(0, SeekOrigin.Begin); 

ImgFileUpload.PostedFile.InputStream.Read(pic, 0, length);

我以前也遇到过类似的情况

对于我的数据库(我正在使用Microsoft Access 2010)的图像列,我的所有图像都是路径~Images2/picture1.jpg。Images2将是存储所有图片的文件夹

对于VS Pro 2013-Grid View Details View等,我将链接到数据库并添加一个Imagefield列,并替换它将显示图像的前一列(Boundfield)

对于fileupload,这是我使用的代码,您可以将其用作参考。如果您想将文件链接存储到您的数据库,然后检索图像以显示在您的网站/应用程序中,您只需将文件另存为“~Images2/picture1.jpg”,然后使用gridview等进行检索。希望对您有所帮助

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            SaveFile(FileUpload1.PostedFile);
        }
        else
        {
            Label4.Text = "You did not specify a file to upload";
        }
    }
    void SaveFile(HttpPostedFile File)
    {
        string savePath = Server.MapPath("~/Images2/");
        string fileName = FileUpload1.FileName;
        string pathToCheck = savePath + fileName;
        string tempFileName = "";

        if (System.IO.File.Exists(pathToCheck))
        {
            int counter = 2;
            while (System.IO.File.Exists(pathToCheck))
            {
                tempFileName = counter.ToString() + fileName;
                pathToCheck = savePath + tempFileName;
                counter++;
            }

            fileName = tempFileName;

            // notify user that file name was changed
            Label4.Text = "A file name with the same name already exist, " +
                "<br />Your file was saved as " + fileName;
        }
        else
        {
            labelFileName.Text = fileName;
            LabelFileType.Text = FileUpload1.PostedFile.ContentType;
            LabelFileContentLength.Text = FileUpload1.PostedFile.ContentLength.ToString();
            Label4.Text = "Your file was uploaded successfully";
        }

        // Append the name of the file to upload to the path
        savePath += fileName;

        FileUpload1.SaveAs(savePath);

    }
}
protectedvoid ImageButton1\u单击(对象发送者,ImageClickEventArgs e)
{
if(FileUpload1.HasFile)
{
保存文件(FileUpload1.PostedFile);
}
其他的
{
Label4.Text=“您没有指定要上载的文件”;
}
}
无效保存文件(HttpPostedFile文件)
{
字符串savePath=Server.MapPath(“~/Images2/”);
字符串文件名=FileUpload1.fileName;
字符串路径检查=保存路径+文件名;
字符串tempFileName=“”;
if(System.IO.File.Exists(pathToCheck))
{
int计数器=2;
while(System.IO.File.Exists(pathToCheck))
{
tempFileName=计数器.ToString()+文件名;
pathToCheck=保存路径+临时文件名;
计数器++;
}
fileName=tempFileName;
//通知用户文件名已更改
Label4.Text=“已经存在同名的文件名,”+
“
您的文件已另存为”+文件名; } 其他的 { labelFileName.Text=文件名; LabelFileType.Text=FileUpload1.PostedFile.ContentType; LabelFileContentLength.Text=FileUpload1.PostedFile.ContentLength.ToString(); Label4.Text=“您的文件已成功上载”; } //将要上载的文件名附加到路径 savePath+=文件名; FileUpload1.SaveAs(savePath); } }
通常最好将图像存储在文件系统中,然后将图像路径存储在数据库中。感谢您的回复,您是否有链接解释您的建议图像以
字节的形式存储在
数据库中。因此,在数据库中看不到图像。将图像存储在文件系统中并不总是一个更好的主意。有时图像太小(例如化身),将其存储为VARBINARY(MAX)更有意义。您不必考虑备份策略,也不必考虑如果文件移动到其他位置,链接可能会断开。FileTables解决了这个问题,但对于小型项目来说,这又是一种过度使用。
image
数据类型将在SQL Server的未来版本中删除。避免在新的开发工作中使用此数据类型,并计划修改当前使用它们的应用程序。改用
varbinary(max)
。为什么建议使用长文本?鉴于图像只是一块二进制数据,VARBINARY(MAX)列更有意义。如果需要的话,该类型还可以更好地存储在单独的列中。我认为利用全文搜索可能会有所帮助。事实上,我确实看了一眼,这似乎与您的数据类型是文本还是字节[]无关。
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            SaveFile(FileUpload1.PostedFile);
        }
        else
        {
            Label4.Text = "You did not specify a file to upload";
        }
    }
    void SaveFile(HttpPostedFile File)
    {
        string savePath = Server.MapPath("~/Images2/");
        string fileName = FileUpload1.FileName;
        string pathToCheck = savePath + fileName;
        string tempFileName = "";

        if (System.IO.File.Exists(pathToCheck))
        {
            int counter = 2;
            while (System.IO.File.Exists(pathToCheck))
            {
                tempFileName = counter.ToString() + fileName;
                pathToCheck = savePath + tempFileName;
                counter++;
            }

            fileName = tempFileName;

            // notify user that file name was changed
            Label4.Text = "A file name with the same name already exist, " +
                "<br />Your file was saved as " + fileName;
        }
        else
        {
            labelFileName.Text = fileName;
            LabelFileType.Text = FileUpload1.PostedFile.ContentType;
            LabelFileContentLength.Text = FileUpload1.PostedFile.ContentLength.ToString();
            Label4.Text = "Your file was uploaded successfully";
        }

        // Append the name of the file to upload to the path
        savePath += fileName;

        FileUpload1.SaveAs(savePath);

    }
}