在 ASP.NET 2.0 中上载文件

分类: asp.net   出处:iocblog整理  更新时间:2009-02-11   添加到收藏  
本页内容
  简介
  fileupload 服务器控件示例
  小结

简介

自引入 microsoft asp.net 版本 1.0 之日起,就存在生成 web 应用程序的内置方法,这些方法能够将文件上载到宿主服务器。这是通过使用 file field html 服务器控件实现的。我以前写过一篇关于如何在 asp.net 应用程序中有效使用该控件的 msdn 文章。本文将再次介绍文件上载过程,但不是使用 file field 控件,我将向您介绍如何有效使用 asp.net 2.0 提供的新 fileupload 服务器控件。

虽然本文向您介绍新增的 fileupload 服务器控件,但现在仍然可以在应用程序中使用 file field 控件,注意到这一点是非常重要的。

fileupload 服务器控件示例

在 asp.net 1.x 中使用 file field 控件时,必须采取一些额外的步骤才能使一切有条不紊地正常运行。例如,您需要亲自将 enctype="multipart/form-data" 添加到页面的 <form> 元素中。asp.net 2.0 中提供的新 fileupload 服务器控件使将文件上载到宿主服务器的过程尽可能的简单。

最后,您试图允许对 html <input type="file">标记进行编程。该标记用于与 html 窗体中的文件数据一起使用。过去使用传统的 asp(asp 3.0 或更早的版本)时,许多程序员使用第三方组件将文件从客户端上载到服务器。现在,通过 .net 和该新控件可以进行上载。清单 1 显示如何使用 fileupload 控件将文件上载到服务器。

提供 microsoft visual basic 和 c# 形式的示例代码。

清单 1. 使用 fileupload 控件将文件上载到服务器

visual basic

<%@ page language="vb" %>
<script runat="server">
    protected sub button1_click(byval sender as object, _
      byval e as system.eventargs)
        if fileupload1.hasfile then
            try
                fileupload1.saveas("c:uploads" & _
                   fileupload1.filename)
                label1.text = "file name: " & _
                   fileupload1.postedfile.filename & "<br>" & _
                   "file size: " & _
                   fileupload1.postedfile.contentlength & " kb<br>" & _
                   "content type: " & _
                   fileupload1.postedfile.contenttype
            catch ex as exception
                label1.text = "error: " & ex.message.tostring()
            end try
        else
            label1.text = "you have not specified a file."
        end if
    end sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >[来源 www.iocblog.net]
<head runat="server">
    <title>upload files</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:fileupload id="fileupload1" runat="server" /><br />
        <br />
        <asp:button id="button1" runat="server" onclick="button1_click"
         text="upload file" /> <br />
        <br />
        <asp:label id="label1" runat="server"></asp:label></div>
    </form>
</body>
</html>

c#

<%@ page language="c#" %>
<script runat="server">
    protected void button1_click(object sender, eventargs e)
    {
        if (fileupload1.hasfile)
            try
            {
                fileupload1.saveas("c:\uploads\" +
                     fileupload1.filename);
                label1.text = "file name: " +
                     fileupload1.postedfile.filename + "<br>" +
                     fileupload1.postedfile.contentlength + " kb<br>" +
                     "content type: " +
                     fileupload1.postedfile.contenttype;
            }
            catch (exception ex)
            {
                label1.text = "error: " + ex.message.tostring();
            }
        else
        {
            label1.text = "you have not specified a file.";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>upload files</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:fileupload id="fileupload1" runat="server" /><br />
        <br />
        <asp:button id="button1" runat="server" onclick="button1_click"
         text="upload file" /> <br />
        <br />
        <asp:label id="label1" runat="server"></asp:label></div>
    </form>
</body>
</html>

运行该页,如果看看为该页生成的源代码,就会注意到一些问题。清单 2 列出这段源代码。

[1] [2] 下一页



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。