码迷,mamicode.com
首页 > Web开发 > 详细

HTML <input type="file">上传文件——结合asp.net的一个文件上传示例

时间:2015-05-27 19:01:01      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

HTML的代码:(关键是要在form里设置enctype="multipart/form-data",这样才能在提交表单时,将文件以二进制流的形式传输到服务器)

一、

<form id="form1" action="test.aspx" method="post" enctype="multipart/form-data">
    <div>
        <input type="file" name="fl" />
        <input type="submit" name="sb" value="submit" />
    </div>
    </form>
public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFile PostedFile = Request.Files[i];
                if (PostedFile.ContentLength > 0)
                {
                    string FileName = PostedFile.FileName;
                    string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1);

                    string sFilePath = Server.MapPath("~/") + FileName;// Server.MapPath("~/123." + strExPrentFile);
                    PostedFile.SaveAs(sFilePath);
                }
                else
                {
                    //this.LabMessage.Text = "不能上传空文件";
                }
            }
            Response.Write("1");
        }
    }
}

 

二、

<form id="form1" runat="server"  method="post" enctype="multipart/form-data">
    <div>
    <input type="file" />
     <asp:Button ID="btnUpload" runat="server"  Text="开始上传" onclick="btnUpload_Click" />
    </div>
    </form>
protected void btnUpload_Click(object sender, EventArgs e)
        {
            //int intCount = RequestClass.GetFormInt("hdcount", -1);
            HttpFileCollection Files = HttpContext.Current.Request.Files;
            for (int i = 0; i < Files.Count; i++)
            {
                HttpPostedFile PostedFile = Files[i];
                if (PostedFile.ContentLength > 0)
                {
                    string FileName = PostedFile.FileName;
                    string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1);

                    string sFilePath = "/uploadfile/hotel/" + StringClass.makeFileName24() + i + strExPrentFile;
                    PostedFile.SaveAs(Server.MapPath(sFilePath));
                }
                else
                {
                    //this.LabMessage.Text = "不能上传空文件";
                }
            }
        }

 

HTML <input type="file">上传文件——结合asp.net的一个文件上传示例

标签:

原文地址:http://www.cnblogs.com/elves/p/4533930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!