当前位置:编程学习 > C#/ASP.NET >>

上传用户头像

如何点击一个按钮,弹出选择文件对话框,选择图片后自动上传并显示在image控件中? --------------------编程问答-------------------- 请查阅上传控件 --------------------编程问答-------------------- --------------------编程问答-------------------- <%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"> 
  protected void Button1_Click(object sender, EventArgs e)
  {
    FileUpload1.SaveAs(Server.MapPath("~/" + System.IO.Path.GetFileName(FileUpload1.FileName)));
    Image1.Visible = true;
    Image1.ImageUrl = "~/" + System.IO.Path.GetFileName(FileUpload1.FileName);
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
  <form id="form1" runat="server">
  <asp:FileUpload ID="FileUpload1" runat="server" />
  <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
  <asp:Image ID="Image1" runat="server" Visible="false" />
  </form>
</body>
</html>
--------------------编程问答-------------------- asp:FileUpoload 控件 --------------------编程问答-------------------- change 事件上写上你上传代码同时替换掉路径 --------------------编程问答-------------------- FileUpload控件上传。使用文件流。 --------------------编程问答--------------------
 <div id="uphead">
                        <div id="myimg">
                            <%-- <asp:Image ID="newPreview" runat="server"  />--%>
                            <img id="newPreview" runat="server" alt="我的头像" src="" />
                        </div>
                        <div id="MyFileUpload1">
                            <asp:FileUpload ID="FileUpload1" runat="server" Height="22px" onchange="showprepic(this.value)"
                                Width="250px" />
                        </div>
                        <asp:HiddenField ID="HiddenField1" runat="server" />
                        注:上传图片必须小于50k,大小为130px*127px为佳</div>


后台
 #region 上传图片

    public string ShowPotole()
    {
        try
        {
            string fileName = "";
            string fileName1 = "";
            if (this.FileUpload1.HasFile)//检查是否有文件
            {
                string fullFileName = this.FileUpload1.PostedFile.FileName;//文件路径名
                fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);        //图片格式

                if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp" || type == "png") //判断是否为图片类型
                {
                    if (this.FileUpload1.PostedFile.ContentLength > 200 * 1024)
                    {
                        Response.Write("<script>alert('上传图片必须小于50k!');</script>");
                        return "";
                    }
                    else
                    {
                        string path = HttpContext.Current.Request.MapPath("~/");//获取上传文件的网站目录路径

                        if (File.Exists(Server.MapPath("~/AmyShow/" + fileName)))//(这个地方做判断是不是 有这 张图片如果已经有图片 则不能上传 )
                        {
                            Response.Write("<script>alert('图片名称已经存在!,不允许上传!');</script>");
                            return "";
                        }
                        else
                        {
                            fileName1 = "AmyShow/" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileName.ToString();
                            this.FileUpload1.SaveAs(path + fileName1);//存储文件到磁盘
                            // this.newPreview.InnerText. = "~/admin/img/" + fileName;//显示图片
                            // this.newPreview.InnerHtml = "~/admin/img/" + fileName;//显示图片
                        }
                    }
                }
                else
                {

                    return "";
                }
            }

            return fileName1;
        }
        catch (Exception)
        {

            throw;
        }
    }
    #endregion
--------------------编程问答-------------------- asp:FileUpoloa 控件 --------------------编程问答--------------------
FileUpload...

uploadify...

swfupload...

http://www.cnblogs.com/zhongweiv/archive/2011/11/29/CutAvatar.html --------------------编程问答-------------------- 貌似HTML5提供了新东西。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,