0

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#.

user3834541
  • 161
  • 1
  • 7
  • 27
  • 1
    What version of .NET is this? In 4.5 you can use `HttpPostedFiles` instead, for earlier versions `HttpPostedFiles` is not an enumerator - you need to enable multiple file upload and use `Request.Files` to iterate over the uploaded files. – Kami Sep 09 '14 at 12:56
  • possible duplicate of [How to choose multiple files using File Upload Control?](http://stackoverflow.com/questions/17441925/how-to-choose-multiple-files-using-file-upload-control) – Kami Sep 09 '14 at 12:57
  • @Kami targetFramework="4.0" – user3834541 Sep 09 '14 at 12:59

4 Answers4

1

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"));

            }
            }
Rajeev Mehta
  • 820
  • 2
  • 10
  • 32
0

Try:

foreach (HttpPostedFile file in FileUpload1.PostedFiles)
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
0
     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)
            {
            }
codebased
  • 6,945
  • 9
  • 50
  • 84
  • umm try with this one then for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; if(file .ContentLength >0){ //saving code here } – codebased Sep 09 '14 at 12:58
  • 1
    Code does not work well in comments - edit the original answer (and mark it as such). Then we can see the formatting/etc. – winwaed Sep 09 '14 at 13:31
0

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.

Lucaci Andrei
  • 398
  • 9
  • 20