簡體   English   中英

從C#/ asp.net中的db加載圖像

[英]Loading an image from db in C#/asp.net

因此,我對代碼進行了重新設計,使其現在可以與眾不同。 但是我的退貨似乎還是很麻煩,還是轉換文件不正確?

C#在頁面加載時調用的方法。

        private void LoadDisplayPhoto()
    {
        var query = (from q in CurrentContext.DisplayPhotos
                     where q.UserID == CurrentUser.UserId
                     select q.Name).FirstOrDefault();
        if (query != null)
        {
            img_adm.ImageUrl = ("~/HandlerFiles/Display.ashx?ImageID=" + CurrentUser.UserId);
        }
        else
        {
            img_adm.ImageUrl = ("~/Content/img/NoProfilePic.jpg");
        }
    }

句柄文件

    public void ProcessRequest (HttpContext context)
{
    byte[] buffer = null;
    string querySqlStr = "";
    //SQL commands.
    if (context.Request.QueryString["ImageID"] != null)
    {
        querySqlStr="select * from DisplayPhotos where PhotoId="+context.Request.QueryString["ImageID"];
    }
    else
    {
        querySqlStr="select * from DisplayPhotos";
    }
    //SQL
    SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WebTest"].ToString());
    SqlCommand command = new SqlCommand(querySqlStr, connection);
    SqlDataReader reader = null;
    try
    {
        connection.Open();
        reader = command.ExecuteReader();
        while (reader.Read())
        {
            //The ID that was requested.
            int id = Convert.ToInt32(context.Request.QueryString["ImageID"]);
            //Gets extenion format.
            var extension = (from q in CurrentContext.DisplayPhotos
                             where q.UserID == id
                             select q.ContentType).FirstOrDefault();

            string name = reader["Name"].ToString();
            int endIndex = name.LastIndexOf('.');
            buffer = (byte[])reader["Data"];
            context.Response.Clear();
            context.Response.ContentType = "image/" + extension;
            context.Response.BinaryWrite(buffer);
            context.Response.Flush();
            context.Response.Close();
        }
        reader.Close();

    }
    finally
    {
        connection.Close();
    }
}

名稱=文件名數據=轉換后的數據。

我上傳文件的方法:

        protected void LinkButton4_Click(object sender, EventArgs e)
    {
        div_editProfile.Visible = true;
        lbl_imgError.Visible = false;
        if (up_ProImg.HasFile)
        {
            int fileLength = up_ProImg.PostedFile.ContentLength;
            string fileName = up_ProImg.PostedFile.FileName;
            string fileType = up_ProImg.PostedFile.ContentType;
            byte[] img = new byte[fileLength];

            var que = from y in CurrentContext.DisplayPhotos
                      where y.Name == fileName
                      select y;
            if (que.Count() > 0)
            {
                lbl_imgError.Visible = true;
                lbl_imgError.Text = "";
            }
            else
            {
                up_ProImg.PostedFile.InputStream.Read(img, 0, fileLength);
                try
                {
                    using (DataContextDataContext udp = new DataContextDataContext())
                    {
                        DisplayPhoto DisP = new DisplayPhoto();
                        DisP.Name = fileName;
                        DisP.ContentType = fileType;
                        DisP.DateLastModified = DateTime.Now;
                        DisP.PhotoId = CurrentUser.UserId;
                        DisP.Data = img;
                        DisP.IsPhoto = true;
                        DisP.IsActive = true;
                        DisP.UserID = CurrentUser.UserId;
                        udp.DisplayPhotos.InsertOnSubmit(DisP);
                        udp.SubmitChanges();
                    }
                }
                finally 
                {
                    var check = from q in CurrentContext.DisplayPhotos
                                where q.Name == fileName
                                select q;

                    if (check.Count() > 0)
                    {
                        lbl_imgError.Visible = true;
                        lbl_imgError.Text = "Image uploaded.";
                    }
                    else
                    {
                        lbl_imgError.Visible = true;
                        lbl_imgError.Text = "Failed to connect to database.";
                    }
                }
            }
        }
        else
        {
            lbl_imgError.Visible = true;
            lbl_imgError.Text = "No image to upload.";
        }
    }

嘗試使用ToList方法。

 var que = from r in CurrentContext.Photos
          select new
          {
              PhotoId = r.PhotoId,
              Name = r.Name,
              PhotoLink = r.SmallThumbnail
          }.ToList()


<asp:ImageButton ID="imgPhoto" runat="server" Width="80px" Height="80px" OnClick="imgPhoto_Click"  ImageUrl="[PhotoLink]"CssClass="PhotoHandle"/>

將[PhotoLink]編輯為適當的數據屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM