【问题标题】:Best way to store image in database in linq to sql [closed]在linq to sql中将图像存储在数据库中的最佳方法[关闭]
【发布时间】:2013-12-13 05:28:52
【问题描述】:

谁能指导将图像存储到数据库的最佳方式?我正在使用 Sql server 2008。目前我将文件路径存储在数据库中,但是当我尝试获取它时,它没有被获取。

这是保存图像的代码:

string Extention = System.IO.Path.GetExtension(fuBanner.FileName);
if (Extention == ".jpg" || Extention == ".jpeg" || Extention == ".bmp" || Extention == ".PNG")
 {
    result.Banner_SignUp_Page = System.IO.Path.GetExtension(fuBanner.FileName);
    db.SubmitChanges();
    lblMsg.Visible = true;
    Response.Redirect("ListOfEvent.aspx");
    //lblMsg.Text = "Records updated successfully.";
 }

以及获取图片的代码:

var result1 = from a in db.EMR_INVITATIONs
    join b in db.EMR_EVENTs on a.EventID equals b.EventID
    where b.EventID == (int)Session["eventid"]   
        select new
                 {
                    Banner = b.Banner_SignUp_Page,
                    Title = b.Title  
                 };

【问题讨论】:

  • 是不是被抓取的意思??可以发一下代码吗?
  • 好的,请稍候我发布代码
  • 也看看debugmode.net/2010/05/10/…这个链接是否有用
  • @F.R.I.E.N.D.S.感谢您的帮助
  • @Monika ,我不确定图像扩展名是否区分大小写,即“.jpg”和“.JPG”是否相同,但请尝试 ToLower() 功能。也许它可以帮助你。 :)

标签: c# asp.net database sql-server-2008 linq-to-sql


【解决方案1】:

在你的情况下:

以下是您想要实现的可能代码:

protected void btnUpload_Click(object sender, EventArgs e)
{     
    if (fuFileUploader.HasFile && fuFileUploader.PostedFile.ContentLength > 0)
    {
        string path_file_name = fuFileUploader.PostedFile.FileName;
        string ext = Path.GetExtension(path_file_name).Replace(".", "");
        string file_name = Path.GetFileNameWithoutExtension(path_file_name);
        string file_title = txtFileTitle.Text.Trim();
        HelperDataClassesDataContext db = new HelperDataClassesDataContext();

        try
        {
            byte[] file_byte = fuFileUploader.FileBytes;
            Linq.Binary file_binary = new Linq.Binary(file_byte);

            ControlDocument cd = new ControlDocument
            {
                guid = Guid.NewGuid(),
                file_ext = ext,
                file_nm = file_name.Trim(),
                file_title=file_title,
                file_bin = file_binary,
                is_active = true,
                upload_page_type = rblLocation.SelectedValue,
                upload_dt = DateTime.Now,
                upload_by = UtilUniverse.Common.CurrentUserLogin.Trim()
            };

            db.ControlDocuments.InsertOnSubmit(cd);
        }
        finally
        {

            db.SubmitChanges();
        }
    }

}

【讨论】:

    【解决方案2】:

    在将图像保存到磁盘之前调整大小的代码。它仅将路径保存在数据库中,而不将图像保存在数据库中。如果我们将路径保存在数据库中,文件保存在磁盘上,那么我们可以在每次必须显示该图像时节省 CPU 成本,(试一试)。您可以使用它来保存项目的任何图像,只需提供上传器控件和图像路径即可。

    public static bool SaveImage(HttpPostedFile imageUpload, string imagePath, out string error)
    {
        error = "";
        string extension = Path.GetExtension(imageUpload.FileName);
        if (extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png")
        {
            try
            {
                System.Drawing.Bitmap objImage = new System.Drawing.Bitmap(imageUpload.InputStream);
                System.Drawing.Bitmap outImage = new Bitmap(170, 200);
                System.Drawing.Graphics outGraphics = Graphics.FromImage(outImage);
                SolidBrush sb = new SolidBrush(System.Drawing.Color.White);
                outGraphics.FillRectangle(sb, 0, 0, 170, 200);
                outGraphics.DrawImage(objImage, 0, 0, 170, 200);
                sb.Dispose();
                outGraphics.Dispose();
                objImage.Dispose();
                outImage.Save(System.Web.HttpContext.Current.Server.MapPath(imagePath));
                return true;
            }
            catch (Exception e)
            {
                error = "Failed to save Image. Error: " + e.Message;
            }
        }
        else
        {
            error = "Invalid image foramt.";
        }
        return false;
    }
    

    然后您可以通过获取图像路径来简单地获取它。

    image.url = //图片列值

    【讨论】:

    • 感谢您的回复。
    • 这不会像用户想要的那样将图像保存在数据库中
    • 是的,它不将图像保存在数据库中,而是将图像保存在磁盘和数据库路径中,虽然用户不想要它,但通过这个用户可能会想到将路径保存在数据库中的好处和磁盘上的图像。
    • @F.R.I.E.N.D.S.,在我的回答中编辑 :)
    • @Sohail 没问题,兄弟,您的解决方案可能对尝试将图像保存在磁盘上的人有所帮助(按照您的建议。)
    【解决方案3】:
     InstituteRegistration insti = new InstituteRegistration(); 
     if (FileUploadInstituteImage.PostedFile.ContentLength != 0)
            {
                string extension =         System.IO.Path.GetExtension(FileUploadInstituteImage.FileName);
                if (extention.ToLower() == ".jpg" || extention.ToLower() == ".jpeg" || extention.ToLower() == ".gif" || extention.ToLower() == ".png" || extention.ToLower() == ".bmp" || extention.ToLower() == ".tif" || extention.ToLower() == ".tiff")
                {
                    Stream fsInstitute = FileUploadInstituteImage.PostedFile.InputStream;
                    BinaryReader brInsti = new BinaryReader(fsInstitute);
                    Byte[] bytesInstitute = brInsti.ReadBytes((Int32)fsInstitute.Length);
                    insti.InstituteImage = bytesInstitute;
                }
                else
                {
                    lblMsg.Text = "Invalid File.";
                    return;
                }
            }
    
     db.InstituteRegistrations.InsertOnSubmit(insti);
     db.SubmitChanges();
    

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 2021-02-11
      • 2011-10-27
      • 2010-10-14
      • 1970-01-01
      相关资源
      最近更新 更多