首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用c#在文件夹上保存多个文件

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

Stack Overflow用户
提问于 2014-09-09 12:50:26
回答 4查看 3.4K关注 0票数 0

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

这是我的ASPX代码:

代码语言:javascript
运行
复制
<%@ 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#代码:

代码语言:javascript
运行
复制
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
            {
            }
        }
    }

我得到了这个错误:

代码语言:javascript
运行
复制
foreach statement cannot operate on variables of type 'System.Web.HttpPostedFile' because 'System.Web.HttpPostedFile' does not contain a public definition for 'GetEnumerator'

在这条线上:

代码语言:javascript
运行
复制
foreach (HttpPostedFile file in FileUpload1.PostedFile)

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

EN

Stack Overflow用户

发布于 2014-09-09 12:59:12

您的错误来自于这样一个事实:您试图在HttpPostedFile对象- 见MSDN上循环。

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

代码语言:javascript
运行
复制
foreach (var postedFile in FileUpload1.PostedFiles)
{
    // do stuff
}

编辑:

PostedFiles属性仅在.Net Framework4.5中有效:

支持的.NET框架: 4.5

PostEdit:

要使该属性正常工作,仅安装.Net Framework4.5是不够的,您必须让您的项目针对.Net Framework4.5(在您的示例中,您的目标是.Net Framework4.0)。

您可以将项目转换为目标.Net框架,方法是进入VS,右键单击项目和应用程序选项卡(应该是默认打开的选项卡),从目标框架下拉菜单中选择所需的框架。

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25745121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档