2016-01-08 243 views
-2

當我上傳圖片到數據庫中時我的圖片採取了這樣的路徑〜/ Images/image.jpg但我會轉換爲此圖片/圖片。 JPG或如何轉換爲圖像/ image.jpg的用C#代碼img標籤的放像下例:如何將圖片從〜/ Images/image.jpg轉換爲圖片/ image.jpg

<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"; 
      } 


      } 

這是我viewcode:

   <% 



        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(); 

        %> 
+0

Server.MapPath更改爲完整路徑:D:/ MyFolder/MyWebsite/Images / – Khazratbek

回答

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)