如何将图像转换为〜/ Images / image.jpg转换为Images / image.jpg

时间:2016-01-08 13:17:47

标签: asp.net

当我将图像上传到数据库时,我的图片采取了这条路径〜/ Images / image.jpg但我会转换为这个Images / image.jpg或如何转换为Images / image.jpg用c#代码放入img标签如下:

 <img src="images/image.jpg"></img>

这是我的上传代码:

       if (FileUpload1.PostedFile != null)
                    {
                      string fileExt =      

System.IO.Path.GetExtension(FileUpload1.FileName);

            if (fileExt == ".jpeg" || fileExt == ".jpg")
            {

                string FileName =        Path.GetFileName(FileUpload1.PostedFile.FileName);

                picpath = FileName;
                //Save files to disk
                FileUpload1.SaveAs(Server.MapPath("Images/" + FileName));


            }
            else
            {
                picpath = "Images/defalut.jpg";
            }


            }

这是我的观看代码:

                <%



                    System.Data.SqlClient.SqlConnection thisConnection = new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
                    SqlCommand thisCommand = thisConnection.CreateCommand();
                    thisCommand.CommandText = "SELECT * from product";
                    thisConnection.Open();
                    SqlDataReader reader = thisCommand.ExecuteReader();

                    while (reader.Read())
                    {
                        string id =Convert.ToString(reader.GetInt32(0));
                        string title = reader.GetString(1);
                        string price = Convert.ToString(reader.GetInt32(4));
                        string  image= reader.GetString(7);

                %>


                    <div class="prod_box">
                    <div class="center_prod_box">            
                        <div class="product_title"><a href="acer.aspx"><%  Response.Write(title);%></a></div>
                        <div class="product_img"><a href="acer.aspx"><img src="<%  Response.Write(image);%>" height="120px" width="120px" alt="" title="" border="0" /></a></div>
                        <div class="prod_price"><span class="reduce"></span> <span class="price"><%  Response.Write(price);%></span></div>                        
                    </div>
                <div class="prod_details_tab">

更多                     

                    <%

                      }
                    thisConnection.Close();

                    %>

1 个答案:

答案 0 :(得分:0)

你可以这样试试。

var outputDir =Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

        var ImageName= Path.Combine(outputDir , "Images\\image.jpg");

        string ImageNew= new Uri(ImageName).LocalPath;

        var imgName= new LinkedResource(ImageNew)