我正在使用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")
{
}
}
}
现在,如果文件名是“文件”,那么我想在页面上自动加载该文件。
我该怎么做?
答案 0 :(得分:0)
如果您的模式是“ {3个字母}-{8个数字}。{Zip}”,则可以使用.Where(f => 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
}
答案 1 :(得分:0)
第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>
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>
STEP2: 在代码页中,例如说(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.";
}
}
}