【问题标题】:How can load file into file component from aspx.cs class?如何将文件从 aspx.cs 类加载到文件组件中?
【发布时间】:2018-10-08 19:44:13
【问题描述】:

我正在使用 ASPX 构建一个简单的页面。

在此页面中,我显示了一个文件组件。使用此组件,用户可以选择本地文件:

<div class="row">
    <form class="form-horizontal">
        <div class="form-group">
            <input type="file" id="selectFile" >
        </div>
    </form>
</div>

现在,我想以编程方式设置此文件。所以从我的 Default.aspx.cs 代码中我有这个:

protected void Page_Load(object sender, EventArgs e)
{
    String s = Request.QueryString["idEsame"];
    //RECUPERO IL FILE ED IL PATH DEL FILE

    string[] fileEntries = Directory.GetFiles("C:\\Users\\michele.castriotta\\Desktop\\deflate_tests");
    foreach (string fileName in fileEntries)
    {
        // here i need to compare , i mean i want to get only these files which are having these type of  filenames `abc-19870908.Zip`
        if(fileName == "file")
        {

        }

    }
}

现在如果文件名是“文件”,那么我想在页面上自动加载这个文件。

我该怎么做?

【问题讨论】:

  • How to Ask。你想要所有的拉链吗? stackoverflow.com/questions/3152157/…
  • 或者可能是一个特定的模式? regex101.com/r/l6AULc/1 。一个简单的 Where 子句就足够了
  • 您是否知道您的代码将在服务器上的目录中查找,而不是在客户端上?
  • Directory.GetFiles 选择服务器端的文件,而不是客户端目录。如果您想允许客户端预览上传的文件,请改用FileUpload 控件。
  • 我想从服务器获取文件。然后在客户端加载这个文件

标签: c# asp.net file


【解决方案1】:

第 1 步:在页面中 (sample.aspx)

插入以下代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sample.aspx.cs" Inherits="sample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;Select File:
        <asp:FileUpload ID="FileUploader" runat="server" />
        <br />
        <br />
        <asp:Button ID="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /><br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label></div>
    </form>
</body>
</html>

第 2 步: 在代码页中说例如(sample.aspx.cs)

插入以下代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class sample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUploader.HasFile)
            try
            {
                FileUploader.SaveAs(Server.MapPath("confirm//") +
                     FileUploader.FileName);
                Label1.Text = "File name: " +
                     FileUploader.PostedFile.FileName + "<br>" +
                     FileUploader.PostedFile.ContentLength + " kb<br>" +
                     "Content type: " +
                     FileUploader.PostedFile.ContentType + "<br><b>Uploaded Successfully";
            }
            catch (Exception ex)
            {
                Label1.Text = "ERROR: " + ex.Message.ToString();
            }
        else
        {
            Label1.Text = "You have not specified a file.";
        }

    }
}

【讨论】:

  • 我不希望代码将文件从客户端上传到服务器,而是将文件从服务器上传到客户端
  • @bircastri 你需要下载吗?
【解决方案2】:

如果您的模式是“{3 个字母}-{8 个数字}.{Zip}”,您可以简单地使用 .Where(f =&gt; myRegex.IsMatch(f)) 过滤文件:

RegexOptions options = RegexOptions.IgnoreCase;
string pattern = @"^\w{3}-\d{8}\.zip$";

string directoryPath = "C:\\Users\\michele.castriotta\\Desktop\\deflate_tests";
var fileEntries = Directory.GetFiles(directoryPath).Where(f => myRegex.IsMatch(f));    

foreach (string fileName in fileEntries)
{
  // Process

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-11
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2017-08-13
    相关资源
    最近更新 更多