简体   繁体   中英

multiple file save on Folder using c#

I want to Save file on Folder without any jquery and javascript and any Plugin. Simply i am using ASP.Net and C#

Here is my ASPX Code :

<%@ 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# Code :

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
            {
            }
        }
    }

I am getting this Error :

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

on this line :

foreach (HttpPostedFile file in FileUpload1.PostedFile)

I want to Save Multiple Files in Folder I am Using ASP.Net and C#.

try this one

  <asp:FileUpload ID="FileUpload1" runat="server" multiple="multiple" />

in .aspx.cs Page

  HttpFileCollection hfc = Request.Files;
  for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            if (hpf.ContentLength > 0)
            {
               hpf.SaveAs(Server.MapPath("Your Path"));

            }
            }

尝试:

foreach (HttpPostedFile file in FileUpload1.PostedFiles)
     foreach(var file in FileUpload1.PostedFiles)
            {
    }

Or

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

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

}

Or

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

Your error comes from the fact that you're trying to loop over an HttpPostedFile object - See MSDN .

You'll need to loop over the PostedFiles property, like so:

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

Edit:

PostedFiles property is valid only in .Net framework 4.5:

.NET Framework Supported in: 4.5

PostEdit:

In order for this property to work it's not enough to have installed the .Net Framework 4.5, you have to have your project targeting the .Net Framework 4.5 (in your case you're targeting .Net Framework 4.0).

You could convert your project to target .Net Framework by going in VS, right clicking on your project and from the Application tab (it should be the default tab opened) selecting the desired framework from the Target framework drop-down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM