回 帖 发 新 帖 刷新版面

主题:ASP.NET文件上传控件FileUpload的一些属性,方法 寻求帮助?

if (FileUpload1.PostedFile != null)
        {
            FileUpload1.Visible = true;
            string strDir = FileUpload1.PostedFile.FileName;
            int myPos = strDir.LastIndexOf("\\");//???????
            string strFileName = strDir.Substring(myPos);
            string strPath = Server.MapPath("./load") + strFileName;
            FileUpload1.PostedFile.SaveAs(strPath);
            Label1.Visible = true;
            this.Label2.Text = "文件名称:";
            this.Label2.Text += FileUpload1.PostedFile.FileName;
            this.Label3.Text = "保存路径:";
            this.Label2.Text += strPath;

            this.Label4.Text = "文件大小:";
            this.Label4.Text += FileUpload1.PostedFile.ContentLength.ToString();
            Label5.Text = "文件类型:";
            Label5.Text += FileUpload1.PostedFile.ContentType;

        }


请问int myPos = strDir.LastIndexOf("\\");//???????
            string strFileName = strDir.Substring(myPos);
是什么意思??



还有就是 我上传大的文件就出错,请问默认上传多大的文件??



谢谢

回复列表 (共2个回复)

沙发


获取文件名,去掉目录的部分。大小没限制,一般需要自己在代码里控制。

板凳


给你一个例子看吧,
前台代码:FileUpload.aspx
--------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="FileUpload" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fUpload" runat="server" Style="z-index: 100; left: 73px; position: absolute;
            top: 63px" />
        <asp:Label ID="lbText" runat="server" Style="z-index: 101; left: 75px; position: absolute;
            top: 105px"></asp:Label>
        <asp:Button ID="btClick" runat="server" OnClick="btClick_Click" Style="z-index: 103;
            left: 328px; position: absolute; top: 61px" Text="上传" />
    
    </div>
    </form>
</body>
</html>
---------------------------------------------------------------------
后台代码:FileUpload.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Drawing;

public partial class FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  
    }
    protected void btClick_Click(object sender, EventArgs e)
    {
        Boolean fileOK = false;
        // 指定路径
        String path = Server.MapPath("~/");
        // 文件上传控件中如果已经包含文件
        if (fUpload.HasFile)
        {
            // 得到文件的后缀
            String fileExtension =
                System.IO.Path.GetExtension(fUpload.FileName).ToLower();
            // 允许的文件后缀
            String[] allowedExtensions = 
                { ".gif", ".png", ".jpeg", ".jpg" };
            // 看包含的文件是否是被允许的文件后缀
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    // 如果是,标志位置为真
                    fileOK = true;
                }
            }
        }

        if (fileOK)
        {
            try
            {
                // 文件另存在服务器指定目录下
                fUpload.PostedFile.SaveAs(path
                    + fUpload.FileName);
                lbText.Text = "文件上传成功!";
            }
            catch (Exception ex)
            {
                lbText.Text = "文件上传失败!";
            }
        }
        else
        {
            lbText.Text = "注意:只能上传gif、png、jpeg或者jpg图像文件!";
            lbText.ForeColor = Color.Red;
        }
    }
}

// 只用到了基本的功能

我来回复

您尚未登录,请登录后再回复。点此登录或注册