关于在.net中预览上传控件中路径的图片的方法

一、在html中:
<SCRIPT language=javascript1.2>
function showsubmenu(sid)
{
    <!--
    function fileChange(file) {
     aspnetForm.face.src=file.value;
    }
    -->
    
</SCRIPT>
 <tr bgcolor="#FFFFFF">
    <td width="15%">&nbsp;&nbsp;图片:</td>
    <td width="85%">
        <%--<asp:Image ID="img" runat="server" ImageUrl="~/ad/ad03.gif" />--%><img id="face" src="~/ad/ad03.gif" width="86"></td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td>&nbsp;&nbsp;图片路径:</td>
    <td>
        <asp:FileUpload ID="FileUpload1" runat="server" onchange="fileChange(this)" />
        <asp:Label ID="lbl" runat="server"></asp:Label></td>
  </tr>
二、在代码中:
 protected void Button3_Click(object sender, EventArgs e)
        {
            string name = FileUpload1.FileName;
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            string type = FileUpload1.PostedFile.ContentType;
            string type2 = name.Substring(name.LastIndexOf(".") + 1);
            string ipath = Server.MapPath("picture")+"\\"+name;
            string fpath = Server.MapPath("upfile") + "\\" + name;
            string wpath="picture\\"+name;
            if (type2 == "gif" || type2 == "jpg" || type2 == "png" || type2 == "bmp")
            {
                FileUpload1.SaveAs(ipath);
                Image1.ImageUrl = wpath;
                Label1.Text = "传送文件名为:" + name + "<br>文件大小为:" + size + "<br>图片类型为:" + type +"<br>后缀名为"+type2+ "<br>实际路径为:" + ipath + "<br>虚拟路径为:" + wpath;
            }
            else
            {
                Image1.Visible = false;
                FileUpload1.SaveAs(fpath);
                Label1.Text = "传送文件名为:" + name + "<br>大小为:" + size + "<br>图片类型为:" + type + "<br>后缀名为" + type2 + "<br>实际路径为:" + fpath + "<br>虚拟路径为:" + wpath;
            }         }
相关文章
相关标签/搜索