使用c#在文件夹上保存多个文件

时间:2014-09-09 12:50:27

标签: c# asp.net asp.net-mvc

我想在没有任何jquery和javascript以及任何插件的文件夹上保存文件。 我只是使用ASP.Net和C#

这是我的ASPX代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="Label1" runat="server" Text="Full Name : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required" ForeColor="Red" ControlToValidate="TextBox1">
    </asp:RequiredFieldValidator><br /><br />
    <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" /><br /><br />
    <asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click" style="margin-bottom:10px;" />
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField HeaderText="Remove">
                <ItemTemplate>
                    <asp:LinkButton ID = "lnkDelete" Text = "Remove" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Text" HeaderText="Image Name" />
            <asp:ImageField DataImageUrlField="Value" HeaderText="Image" ControlStyle-Height="100" ControlStyle-Width="100" />
        </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
</asp:Content>

C#代码:

protected void Button1_Click(object sender, EventArgs e)
    {
        DateTime curr = DateTime.Now;
        DateTime INDIAN_ZONE = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(curr, "India Standard Time");

        if (FileUpload1.HasFile)
        {
            foreach (HttpPostedFile file in FileUpload1.PostedFile)
            {
                string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
                string directoryPath = Server.MapPath(string.Format("./upload/" + TextBox1.Text));
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                else
                {
                }

                string fileName = Path.GetFileName(file.FileName);
                fileName = time1 + fileName;
                string path = "./upload/" + TextBox1.Text + "/";
                file.SaveAs(Server.MapPath(path) + fileName);
            }

            //GridView1 Bind with attach file
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/upload/" + TextBox1.Text + "/"));
            List<ListItem> files = new List<ListItem>();
            foreach (string filePath in filePaths)
            {
                string fileName1 = Path.GetFileName(filePath);
                files.Add(new ListItem(fileName1, "~/upload/" + TextBox1.Text + "/" + fileName1));
            }
            GridView1.DataSource = files;
            GridView1.DataBind();
        }
        else
        {
            string time1 = INDIAN_ZONE.ToString("MM-dd-yyyy_hhmmss");
            string directoryPath = Server.MapPath(string.Format("./upload/" + TextBox1.Text));
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
            else
            {
            }
        }
    }

我收到此错误:

foreach statement cannot operate on variables of type 'System.Web.HttpPostedFile' because 'System.Web.HttpPostedFile' does not contain a public definition for 'GetEnumerator'

在这一行:

foreach (HttpPostedFile file in FileUpload1.PostedFile)

我想在文件夹中保存多个文件 我正在使用ASP.Net和C#。

4 个答案:

答案 0 :(得分:1)

尝试这个

  <asp:FileUpload ID="FileUpload1" runat="server" multiple="multiple" />
<{1>}页面

中的

.aspx.cs

答案 1 :(得分:0)

尝试:

foreach (HttpPostedFile file in FileUpload1.PostedFiles)

答案 2 :(得分:0)

     foreach(var file in FileUpload1.PostedFiles)
            {
    }

for (int i = 0; i < Request.Files.Count; i++) 
{ HttpPostedFileBase file = Request.Files[i]; 

          if(file .ContentLength >0){ //saving code here }

}

或者

   foreach (var file in System.Web.HttpContext.Current.Request.Files)
            {
            }

答案 3 :(得分:0)

您的错误来自于您尝试遍历HttpPostedFile对象的事实 - See MSDN

您需要遍历PostedFiles属性,如下所示:

foreach (var postedFile in FileUpload1.PostedFiles)
{
    // do stuff
}

修改

PostedFiles属性仅在.Net framework 4.5中有效:

  

.NET Framework   受以下支持:4.5

<强> PostEdit:

为了使这个属性能够运行它还不足以安装.Net Framework 4.5,你必须让你的项目针对.Net Framework 4.5(在你的情况下,你的目标是.Net).Net框架4.0)。

您可以通过进入VS,右键单击项目并从“应用程序”选项卡(应该是打开的默认选项卡),从Target框架下拉列表中选择所需的框架,将项目转换为目标.Net Framework。 / p>