注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Blue的浪漫-分享快乐-分享美丽

感谢上苍我所拥有的…感谢上苍我所没有的…

 
 
 

日志

 
 
关于我

一个热爱祖国.热爱生活.兴趣爱好广泛的80后小女子~~.《渴望浪漫博客》博客前八名之列。欢迎朋友们加我博友!

网易考拉推荐

使用ASP.NET上传单个多张图片汇总,  

2012-05-13 20:07:31|  分类: 网站编程类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1, 使用标准HTML来进行图片上传

前台代码:

<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td colspan="2" style="height: 21px" >
                    使用标准HTML来进行图片上传</td>
            </tr>
            <tr>
                <td style="width: 400px">
                    <input id="InputFile" style="width: 399px" type="file" runat="server" /></td>
                <td style="width: 80px">
                    <asp:Button ID="UploadButton" runat="server" Text="上传图片" OnClick="UploadButton_Click" /></td>
            </tr>
            <tr>
                <td colspan="2" >
                    <asp:Label ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td>                
            </tr>
        </table>    
    </div>
    </form>
</body>
后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名
        //string uploadName = InputFile.PostedFile.FileName;
        string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
        if (InputFile.Value != "")
        {
            int idx = uploadName.LastIndexOf(".");
            string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名
            pictureName = DateTime.Now.Ticks.ToString() + suffix;
        }
        try
        {
            if (uploadName != "")
            {
                string path = Server.MapPath("~/images/");
                InputFile.PostedFile.SaveAs(path + pictureName);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
}


2 单文件上传

        这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用。其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本。

前台代码:

<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 90%">
            <tr>
                <td style="width: 159px" colspan=2>
                    <strong><span style="font-size: 10pt">最简单的单文件上传</span></strong></td>
            </tr>
            <tr>
                <td style="width: 600px">
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="600px" /></td>
                <td align=left>
                    <asp:Button ID="FileUpload_Button" runat="server" Text="上传图片" OnClick="FileUpload_Button_Click" /></td>
            </tr>
            <tr>
                <td colspan=2>
                    <asp:Label ID="Upload_info" runat="server" ForeColor="Red" Width="767px"></asp:Label></td>
            </tr>
        </table>    
    </div>
    </form>
</body>
后台代码:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void FileUpload_Button_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.PostedFile.FileName == "")
            //if (FileUpload1.FileName == "")
            //if (!FileUpload1.HasFile)     //获取一个值,该值指示 System.Web.UI.WebControls.FileUpload 控件是否包含文件。包含文件,则为 true;否则为 false。
            {
                this.Upload_info.Text = "请选择上传文件!";
            }
            else
            {
                string filepath = FileUpload1.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名,如:C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
                //string filepath = FileUpload1.FileName;               //得到上传的文件名20022775_m.jpg
                string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg
                string serverpath = Server.MapPath("~/images/") + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg
                FileUpload1.PostedFile.SaveAs(serverpath);//将上传的文件另存为
                this.Upload_info.Text = "上传成功!";
            }
        }
        catch (Exception ex)
        {
            this.Upload_info.Text = "上传发生错误!原因是:" + ex.ToString();
        }
    }
}


 

  3 多文件上传

前台代码:

<body>
    <form id="form1" runat="server">
    <div>
    <table style="width: 343px">
            <tr>
                <td style="width: 100px">
                    多文件上传</td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="475px" />
                    </td>
                <td style="width: 100px">
                    </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload2" runat="server" Width="475px" /></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload3" runat="server" Width="475px" /></td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="一起上传" />
                    <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="448px"></asp:Label></td>
                <td style="width: 100px">
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
 后台代码:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void bt_upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "" && FileUpload3.PostedFile.FileName == "")
        {
            this.lb_info.Text = "请选择文件!";
        }
        else
        {
            HttpFileCollection myfiles = Request.Files;
            for (int i = 0; i < myfiles.Count; i++)
            {
                HttpPostedFile mypost = myfiles[i];
                try
                {
                    if (mypost.ContentLength > 0)
                    {
                        string filepath = mypost.FileName;//C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg
                        string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg
                        string serverpath = Server.MapPath("~/images/") + filename;//C:\Inetpub\wwwroot\WebSite2\images\20022775_m.jpg
                        mypost.SaveAs(serverpath);
                        this.lb_info.Text = "上传成功!";
                    }
                }
                catch (Exception ex)
                {
                    this.lb_info.Text = "上传发生错误!原因:" + ex.Message.ToString();
                }
            }
        }
    }
}




电脑知识大全
电脑知识
博客资源素材-大全
资源素材
精品大全
精品大全
职场技能大全
职场技能
两性天地大全
两性天地
生活技巧大全
生活技巧
美容打扮大全
美容打扮
欧默搞笑大全
欧默搞笑
音乐伴舞大全
音乐伴舞
饮食知识 医疗保健 哲理感悟
博客素材 复制代码 渴望浪漫
程序代码    
本博首页
喜欢就搬
  评论这张
 
阅读(325)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017