简体   繁体   English

使用c#将多个文件保存到Folder

[英]multiple file save on Folder using c#

I want to Save file on Folder without any jquery and javascript and any Plugin. 我想在没有任何jquery和javascript和任何插件的情况下将文件保存在Folder上。 Simply i am using ASP.Net and C# 我只是在使用ASP.Net和C#

Here is my ASPX Code : 这是我的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# Code : 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
            {
            }
        }
    }

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#. 我想在使用ASP.Net和C#的文件夹中保存多个文件。

try this one 试试这个

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

in .aspx.cs Page .aspx.cs页中

  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 . 您的错误来自于您试图遍历HttpPostedFile对象的事实- 请参见MSDN

You'll need to loop over the PostedFiles property, like so: 您需要像这样循环遍历PostedFiles属性:

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

Edit: 编辑:

PostedFiles property is valid only in .Net framework 4.5: 已发布文件属性仅在.Net Framework 4.5中有效:

.NET Framework Supported in: 4.5 .NET Framework受以下版本支持:4.5

PostEdit: 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). 为了使此属性起作用,仅安装.Net Framework 4.5是不够的,您必须使项目面向.Net Framework 4.5(在您的情况下,您针对.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. 您可以通过进入VS,右键单击您的项目,然后从“应用程序”选项卡(应该是打开的默认选项卡)中,从“目标”框架下拉列表中选择所需的框架,将项目转换为目标.Net Framework。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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