How to assign a file to file upload in server side c# (asp.net website)

2.7k Views Asked by At

I have a asyncfile upload in my asp.net website. I have to show a file name inside the file upload. I have a edit functionailty which has to display the name of uploaded file insided the file upload. I just want to show the file name which i have it during page load, How can I assign the file name to the file upload in server side(c#) And how to change the backcolor of the file upload from server side.

 <asp:AsyncFileUpload ID="AFU_Doc"  ErrorBackColor="Red" OnUploadedComplete="btnDocUpload_Click"
        runat="server" UploaderStyle="Traditional" ThrobberID="aajaxLoader" />
2

There are 2 best solutions below

0
On

That is not possible. With AsyncFileUpload, the FileName property is read-only.

I assume this is to avoid hidden file uploads.

0
On

For Security reason we can't assign file name to file upload control in html or i asp.net

To achieve this assign a label in place of file upload control and bind the file name to label and hide the file upload control

IF user want to upload new file then assign delete image near it and on click hide label and show file upload control and upload the file

You can hide and show following control as per your requirement in client side or sever side.

<input type="file" id="txtFileUploadGrid" runat="server" />

<asp:Label ID="lblFromFileName1" runat="server"></asp:Label>

<asp:ImageButton ID="btnRemove" CommandName="Remove" runat="server" ToolTip="Remove Zip" ImageUrl="~/CommonContent/Images/delete.png" />