user3017144
user3017144

Reputation: 11

C# Get and Set property programmatically on FileUpload1.PostedFile

I have a file upload control on my asp.net mobile application. My requirement is while accessing a local folder path from browser; I need to set the path programmatically. And don’t allow user to brows the file.

My question is, when I try to access File-upload Control property name like -> Posted-file That will not allow me to set the values programmatic. That will allow read-only currently.
It is possible to set the values programmatically on file-upload control?
Or Is there another way to expose the FileUpload1.PostedFile property using like web User Control?

Exp:

public partial class TestWebUserControl : System.Web.UI.UserControl
{
public System.Web.HttpPostedFile PostedFileText
{            
get { return FileUpload1.PostedFile; }
//set { FileUpload1.PostedFile = value; }
}
}

Upvotes: 1

Views: 4537

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176936

you cannot set value directly to the file post

There is no direct solutions as mentioned to set value of file upload control after postback in asp.net. Even if you tried to set the value of FileUpload control through javascrip, that will not work because of security restrictions.

You can't also set the FileUpload value on the server because the FileName and other attributes is readonly.

solution:

You can use:

label control to display the last selected file .
hiddenField Control to transfer the selected file from the client to server.

but you need also to remeber the last posted file because in case you don't select a file from FileUpload control and then posted back, you will notice that the FileUpload.postedFile value will be gone , so you have to save the posted file somewhere. See this for an example;

ASPX Code

<form id="form1" runat="server">  
       <asp:FileUpload ID="FileUpload1" runat="server" /> <br />  
        <asp:Label ID="lblCurrentFile" runat="server"></asp:Label><br />  
       <br />  
       <asp:Button ID="BtnSubmit" runat="server" Text="postBack" />  
       <br />  
       <asp:HiddenField ID="HiddenField1" runat="server" />  
</form>  

find full solution |: http://www.nullskull.com/q/10140208/we-cant-set-value-of-file-upload-control-after-postback-in-aspnet.aspx

Upvotes: 2

Murali Murugesan
Murali Murugesan

Reputation: 22619

Why do you want to modify the Request? You cant change it.

If you want you read it and assign it in a local variable(byte[]).

byte[] buffer = new byte[FileUpload1.PostedFile.InputStream.Length];
FileUpload1.PostedFile.InputStream
 .Read(buffer, 0, FileUpload1.PostedFile.InputStream.Length);

Upvotes: 0

Related Questions