-2

I'm building a simple page with ASPX.

In this page I display a file component. With this component, the user can select a local file:

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

Now, I want to set this file programmatically. So from my Default.aspx.cs code I have this:

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

        }

    }
}

Now if the filename is "file" then I want to load automatically this file on the page.

how can I do this?

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
bircastri
  • 2,169
  • 13
  • 50
  • 119
  • [ask]. You want all zip ? https://stackoverflow.com/questions/3152157/find-a-file-with-a-certain-extension-in-folder – Drag and Drop Oct 08 '18 at 09:30
  • Or perhaps a specific patern? https://regex101.com/r/l6AULc/1 . A simple Where clause should be enought – Drag and Drop Oct 08 '18 at 09:32
  • Are you aware that your code will look in a directory on the server, not on the client? – user247702 Oct 08 '18 at 09:32
  • `Directory.GetFiles` picks files on the server-side, not client-side directories. If you want allowing client to preview uploaded file, use `FileUpload` control instead. – Tetsuya Yamamoto Oct 08 '18 at 09:33
  • I want to get a file from server. Then load this file on the client – bircastri Oct 08 '18 at 09:34
  • @bircastri, You said "load this file on the client", But what if there is multiple file matching this pattern? You can't have multiple response to one request. related question [here](https://stackoverflow.com/questions/22413679/how-to-download-multiple-files-using-asp-net) – Drag and Drop Oct 08 '18 at 09:48
  • @bircastri, have you try my answer? Does it help? Is the pattern ok? Do you need clarification? – Drag and Drop Oct 08 '18 at 11:46

2 Answers2

0

If your pattern is "{3 letter}-{8 numbers}.{Zip}", you can simply filter the files using .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

}
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
0

STEP1: IN THE PAGE ( sample.aspx)

INSERT THE FOLLOWING CODE:

<%@ 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>

STEP2: in the code page say for example ( sample.aspx.cs)

INSERT THE FOLLOWING CODE:

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.";
        }

    }
}
Harold Sota
  • 7,490
  • 12
  • 58
  • 84