Dear All,
Have good day,

I am trying to attatch file like yahoomail attatchement ,it means displaying name of file and Remove link after Button_click event fire.

protected void Page_Load(object sender, EventArgs e)
    {
        LinkButton1.Visible = false;
        label2.Visible = false;
    }
if (FileUpload1.HasFile)
            try
            {
                FileUpload1.SaveAs("e:\\parimal\\" + FileUpload1.FileName);
                Label1.Text = "File name: " + FileUpload1.PostedFile.FileName + "<br>" +
                     FileUpload1.PostedFile.ContentLength + " kb<br>" +
                     "Content type: " +
                     FileUpload1.PostedFile.ContentType;

                flag = 1;
            }
            catch (Exception ex)
            {
                Label1.Text = "ERROR: " + ex.Message.ToString();
            }
        else
        {
            Label1.Text = "You have not specified a file.";
        }
        if (flag == 1)
        {
            label2.Text = FileUpload1.PostedFile.FileName;
            label2.Visible = true;
            LinkButton1.Visible = true;
            flag = 0;
        }

Its work properly for one file ,but if I want to add one then it not work
so plz if U have solution for displaly all the file dynamicallly below of "FILE" control in asp.net
Thanks in Advanced....

Dani AI

Generated

As discovered, wiring the FileUpload input's onchange to submit (as suggested by ) solves the "click Open and upload immediately" behavior. To extend that into a Yahoo‑style attachments panel (file name + size + a Remove link for multiple files) treat each onchange as a small, incremental upload and keep metadata on the server so the UI can be re-rendered after each upload.

A practical workflow: on each onchange save the incoming file into a dedicated temporary uploads folder under a unique server name, then add a metadata record (original name, server filename, size, MIME, timestamp) to a short‑lived store (Session list, temp DB table or cache). Render a compact attachments panel below the file control by iterating that store — show the original filename and size, and output a Remove control that posts back or calls a small AJAX endpoint to delete the server file and remove the metadata entry. Use a hidden field or JSON payload if a stateless approach is preferred.

Important cautions and UX notes: do not hard‑code drive paths (avoid things like E:\parimal); resolve a virtual uploads folder with Server.MapPath/MapPath and give that folder non‑executable write permissions. Always validate extension, MIME and size server‑side, generate unique server filenames (GUID or timestamp) to avoid collisions, HTML‑encode displayed filenames to prevent XSS, and implement periodic cleanup of orphaned temp files. For modern browsers, the HTML5 multiple input + FormData or a client uploader library (jQuery File Upload / similar) provides a smoother experience than trying to juggle partial postbacks.

Common traps: FileUpload will return no file on partial/async postbacks unless special handling is used; ensure the form is multipart/form‑data; check IIS/web.config request size limits and folder permissions when uploads silently fail. The PHP example from illustrates filename handling concerns but the above WebForms workflow keeps uploads predictable and removable.

Recommended Answers

All 5 Replies

>displaly all the file dynamicallly below of "FILE" control in asp.net

Sorry, I'm not quite following the problem. There is no "FILE" control in asp.net.

>Uploading file and dispaly name of flie and remove Link.

You may use System.Directory.GetFiles(path) method to have a list of files.

>displaly all the file dynamicallly below of "FILE" control in asp.net

Sorry, I'm not quite following the problem. There is no "FILE" control in asp.net.

>Uploading file and dispaly name of flie and remove Link.

You may use System.Directory.GetFiles(path) method to have a list of files.

Oh....no ,,actully its Fileupload control in asp.net that is used to uploading file....
and i am able to do attach file by click event on Button.but i want this can done by only click on OPEN button of DialogBox and not on asp server control like Button.

if U understand then plz reply me as u can........

>but i want this can done by only click on OPEN button of DialogBox and not on asp server control like Button.

Subscribe JavaScript event onchange.

.aspx page

<head runat="server">
    <title>Sample Page</title>
    <script type="text/javascript">
       function DoSubmit(){
           var form=document.getElementById("form1");
           form.submit();
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server"  onChange="DoSubmit();"  />
    </div>
    </form>
</body>

.cs code-behind file

protected void Page_Load(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(MapPath("~/folderName/" + FileUpload1.FileName));
        }
    }

Wow.......,
Thank you ,Thank you So much MR.adatapost.
IT work very well .....and i am very very happy to successing it....
thank u so much once again.......

>but i want this can done by only click on OPEN button of DialogBox and not on asp server control like Button.

Subscribe JavaScript event onchange.

.aspx page

<head runat="server">
    <title>Sample Page</title>
    <script type="text/javascript">
       function DoSubmit(){
           var form=document.getElementById("form1");
           form.submit();
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server"  onChange="DoSubmit();"  />
    </div>
    </form>
</body>

.cs code-behind file

protected void Page_Load(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(MapPath("~/folderName/" + FileUpload1.FileName));
        }
    }

Hi

You can refer following code for Uploading file and dispaly name of flie and remove Link.


<?php

// Your file name you are uploading
$file_name = $HTTP_POST_FILES;

// random 4 digit to add to our file name
// some people use date and time in stead of random digit
$random_digit=rand(0000,9999);

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables

$new_file_name=$random_digit.$file_name;

//set where you want to store files
//in this example we keep file in folder upload
//$new_file_name = new upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES, $path))
{
echo "Successful<BR/>";

//$new_file_name = new file name
//$HTTP_POST_FILES = file size
//$HTTP_POST_FILES = type of file
echo "File Name :".$new_file_name."<BR/>";
echo "File Size :".$HTTP_POST_FILES."<BR/>";
echo "File Type :".$HTTP_POST_FILES."<BR/>";
}
else
{
echo "Error";
}
}
?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.