本文介绍了如何在C#代码中传递图像URL但图像不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何上传图片文件上传器,图片保存在服务器路径中并在图片中传递图片网址但图片未显示

how do i upload a image file uploader, image save in server path and pass image url in image but image not show

<table>
                       <tr>
                           <td width="210px" height="160px">
                               <asp:Image ID="imgMartyr" runat="server" Width="149px" Height="166px" ImageUrl="~/images/noimage.jpg" />
                           </td>
                       </tr>
                       <tr>
                           <td height="20px" style="text-align: center">
                               <asp:FileUpload ID="fuImageMartyr" runat="server" Width="100px" Height="24px" />
                               <asp:Button ID="btnImgMartyr" runat="server" Width="100px" Height="24px" OnClick="btnImgMartyr_Click" Text="Upload" />
                           </td>
                       </tr>
                   </table>





protected void btnImgMartyr_Click(object sender,EventArgs e)

{

if(fuImageMartyr.HasFile)

{

if(fuImageMartyr.PostedFile.ContentLength> 20480)

{

ShowMessage(图像大小不大于20 KB);

}

else

{

string path = Server.MapPath(../ images /)+ fuImageMartyr.FileName;

fuImageMartyr.SaveAs(path );

ViewState [Filename] = fuImageMartyr.FileName;

Stream fs = fuImageMartyr.PostedFile.InputStream;

BinaryReader br = new BinaryReader(fs);

Byte [] bytes = br.ReadBytes((Int32)fs.Length);

ViewState [Image] = bytes;

imgMartyr.ImageUrl = path;

ShowMessage(图片上传成功);

}

}

}



protected void btnImgMartyr_Click(object sender, EventArgs e)
{
if (fuImageMartyr.HasFile)
{
if (fuImageMartyr.PostedFile.ContentLength > 20480)
{
ShowMessage("Image size not grater than 20 KB");
}
else
{
string path = Server.MapPath("../images/") + fuImageMartyr.FileName;
fuImageMartyr.SaveAs(path);
ViewState["Filename"] = fuImageMartyr.FileName;
Stream fs = fuImageMartyr.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
ViewState["Image"] = bytes;
imgMartyr.ImageUrl = path;
ShowMessage("Image uploaded successfully");
}
}
}

推荐答案

string path = Server.MapPath("../images/") + fuImageMartyr.FileName;
fuImageMartyr.SaveAs(path);
ViewState["Filename"] = fuImageMartyr.FileName;
Stream fs = fuImageMartyr.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
ViewState["Image"] = bytes;

try this snippet:
imgMartyr.ImageUrl = "../images/" + fuImageMartyr.FileName;

ShowMessage("Image uploaded successfully");


string path = Server.MapPath("../images/") + fuImageMartyr.FileName;
fuImageMartyr.SaveAs(path);
ViewState["Filename"] = fuImageMartyr.FileName;
Stream fs = fuImageMartyr.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
ViewState["Image"] = bytes;

ShowMessage("Image uploaded successfully");





试试这个片段:

imgMartyr.ImageUrl =.. / images /+ fuImageMartyr.FileName;



try this snippet:
imgMartyr.ImageUrl = "../images/" + fuImageMartyr.FileName;


这篇关于如何在C#代码中传递图像URL但图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 07:12