Browse file dialog - asp.net

How can I add a file browse functionality on my asp.net web form ?
I have a similar button in my winforms app and now I need it on asp.net my web page.
FileUpload control is used to select and post file to a web server, I just need to select file path/name without sending file.
Should I add some JS to delete FORM item before doing POST or any other simplier solution exists ?

Follow this.
You can use asp:FileUpload , when user selects a file, you need to get that path using this - document.getElementById("fileeupload_id").value. Save this value to hidden field , say with id hdn.
Reset file selection using this - document.getElementById("fileeupload_id").value = "".
So that, when page will be posted, you file will not be posted,rather filename will be posted as hdn.Use Request.Form["hdn"] to get selected file path.

Related

Dynamic destination with AjaxFileUpload

I am using an AjaxFileUpload for multiple file upload. It is working, but I want to change the destination with the value in a textbox. The code is below which is in the AjaxFileUpload1_onUploadComplete method:
string myDir= myDirTextBox.Text.Trim();
AjaxFileUpload1.SaveAs(Server.MapPath("../allarticles/"+ myDir+"/"+e.FileName));
I debug the project and write a name in the myDirTextBox. Then when I click the Upload File button the value of this textbox is always null. That's why I can't change the destination dynamically.
I have read this article but it is not clear: upload multiple files with ajaxFileUpload with different file ids
What is the problem and how can I solve it?
When server event occurs the page goes through complete page lifecycle.
That means your code deals with new, uninitialized version of a page which is not related in any way with the page you see in a browser.
To save the value of a textbox and utilize it later you need to write some code to store this value between page requests. This can be simple JavaScript code attached to Upload button, that posts textbox value with AJAX to a server. Server, in turn, will store this value in session or another persistent storage.

Asp.net fileUploader(brettle.web) is not working on post back

I am using a third party file uploader which is same as asp.net file uploader control.
On a page before post back(clicking a button in gridview) the file name gets empty from file uploader and on code behind can't get file.
There were update panels on page. I have also removed them but no affect.
I've checked the Brettle.Web FAQ http://neatupload.codeplex.com/documentation and found the following answer to a very similar problem:
Q: Why are filenames cleared on postback?
A: Browser security restrictions prevent the server from set the names of files to upload.

The name does not exist in the current context

I have a masterpage in my asp.net 3.5 application and I have some controls and jquery stuff. I try to access the controls in codebehind and it says :
The name 'DrpStates' does not exist in the current context
Why it is not accessible in codebehind ?
When you create a code behind file, ASP.NET also automatically generates a designer file (which is right next to it). In that designer file all the controls are initialized and loaded. Sometimes (for reasons unknown) when you create a new control, it fails to re-initialize the designer file and you can't get access to the control in the code behind file.
Try doing this >
Delete the designer file (right click > delete)
Right click on the aspx file > Convert to Web Application
Should work now
It's probably part of the master page or parent page, try using FindControl method:
this.Page.FindControl("DrpStates");
There could be a problem with your .designer.cs file. Check if you have a designer file with the same name as your aspx (or ascx) file.
If you open the aspx file and switch between design view and html view and back it will prompt VS to check the controls and add any that are missing to the designer file.
Try right clicking on the aspx and select "Convert to Web Application".
You can also try deleting the .designer.cs file and then recreate an empty file with the same name.
reason : - When we create a code behind file, ASP.NET also automatically generates a designer file. In that designer file all the controls are initialized and loaded. Sometimes when we create a new control, it fails to re-initialize the designer file and you can't get access to the control in the code behind file.
There is a simple Solution to this situation.
Step1 : open the the yourfile.aspx.designer.cs file
you will find things like " protected global::System.Web.UI.WebControls.Label Label2; "
these are the initialized components in the sequence in which they were generated by you.
Step2: just copy and paste the following line repeatedly for every missing component that were not
recognized by the code behind : "global::System.Web.UI.WebControls."+Class of the component that you are missing + single space + id of the missing component.
Step3: save the file and voila all the components error disappear magically.

Problem with saving attached files using FileUpload control in ASP.NET

In my website,I have a home page in which by clicking on an image button,I get redirected to a different .apsx page .This page consists of a FileUpload control and 2 buttons(ATTACH,CANCEL).When a user selects/browses a file from his local machine and click ATTACH button,I display that file in a GRIDVIEW and also push the details of that file like Filename into a DATATABLE.
The user in this way can browse multiple files and all of them are added to GridView and also pushed to DATATABLE.Now when the user clickes CANCEL button,I am sending the whole DATATABLE in a session object to the HOME page.Upon clicking SAVE button in the home page,the files in the DATATABLE must get stored in a physical location that I mention in the code.
The problem that I am facing is that when I write
FileUpload fl=new FileUpload;
fl.SaveAs(dt["fileName"]);
The files are not at all getting saved in the location.
However If I pass the FileUpload control using Session from the second page,
FileUpload fl=(FileUpload)Session["FileUpload"]
The files are getting saved with the correct filenames but the content of all the files consists of the content of the latest uploaded file.I know what the problem is but unable to get a solution.
My Requirement is to save the files in a physical path only after clicking the Save button in the home page.Kindly Suggest me..Thanks in advance!
I believe that the asp.net FileUpload control only supports uploading of one file at a time. that is why you only see the contents of the last file. You either need to create a separate control for each upload, roll your own, or use a 3rd-party vendor control.

Maintaining ViewState for FileUpload Control

I am creating FileUpload controls at run time. When I click a LinkButton a new FileUpload control is generated.
Now suppose I have selected a file from a FileUpload control and I click the LinkButton. The previous FileUpload control loses its path. However, I'm maintaining the ViewState of each control that I create at runtime by using this line:
f1.enableviewstate = true;
How do I maintain the selected file for a FileUpload control?
Steps
user selects a file
user click LinkButton (issues a postback that adds additional file uploading control)
server side should get the file on postback and store it somewhere (anywhere)
replace first <input type=file> with something like Label and check mark icon (to tell user the file has already been uploaded (or even a read-only text box with disabled browse button to fake file upload control - however you won't be able to display correct file path in it)
user is presented with a new form that has new empty file upload control in showing already uploaded files.
For security reasons you can't manipulate <input type=file> in any way shape or form.
Hack approach
If I understood you correctly your link button adds additional file upload controls to your page. Instead I'd create a sufficient number of upload controls the first time and display just one. Others would be hidden by CSS. After user clicks the LinkButton, it would however have only client-side Javascript functionality that would reveal additional control. And another... and another... and another... until maximum is reached.
Complex approach
You could however make it in a different way by using more Javascript and make it more Web 2.0-ish. You should however upload those files via <iframe>
as some of the others mentioned, you cannot preserve the viewstate of a FileUpload due to security issues.
What you could do is to simply add a Label just below the FileUpload. When the user clicks on the linkbutton in order to generate a new FileUpload, a postback will be fired where you could check whether the FileUpload controls present on the page have some value (i.e. the user already selected a file to upload), and if so, you could directly start to upload that file and show the result (the path or filename) on the label, just that the user knows he has added that file already. You could also hide the fileupload and additionally add a remove link to again remove the uploaded file (similar approach as Gmail does).
Hope this helped.
Juri
You can't pre-select a file path in the file upload input tag (security related - the user must select the file), so .Net is not able to populate the value from viewstate.
Consider if you really need to at it at runtime?
If you really need to at runtime; Don't forget to add it to the closest container's Controls property. Doing this makes sure it's state is serialized to the ViewState.
Hope this helps...
as per me there is no way to persist viewstate of fileupload in asps.net.
u can store it's value in hidden field,session etc file u can not be able to assign that value to again file upload because it is read only

Resources