Dynamic destination with AjaxFileUpload - asp.net

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.

Related

AJAX AjaxFileUpload control getting list of uploaded files when Submit ASP.NET web form

I have an ASP.NET web form to get name, email address and uploaded filename(s). Added an AjaxFileupload control (ASP.NET AJAX Toolkit v16.1 not to be confused with AsyncFileupload or ASP.NET FileUpload control) that works OK because it lets you upload files without doing any Postback at all avoiding to submit the same form more than one time (See sample here: Upload files and images without page PostBack / Refresh / Reload in ASP.Net - )
The control works fine, it saves the files but the problem is: once the fields in the form are validated, the file(s) are uploaded and I'm finally ready to Submit the form I can't get the path of the uploaded files because the AjaxFileUpload control doesn't have any method to do that :(
In other words you can't simply invoke AjaxFileUpload.GetListOfUploadedFiles() and get an ArrayList of uploaded files. I want to save all the fields on a record, name, email and all the filenames that this user has been uploaded so I can use this data to make a report.
Protected Sub OnUploadComplete(sender As Object, e As AjaxFileUploadEventArgs)
Dim fileName As String = Path.GetFileName(e.FileName)
Me.AjaxFileUpload1.SaveAs(Server.MapPath("/output/" & fileName))
End Sub
I've tried to catch the filenames there but once one file gets saved and you left the Sub the data is gone, so here is my question:
How can I do to get a String with all the files listed on the "uploaded files" section of the AjaxFileUpload control so when I Submit the form I can get all those filenames?
I've seen something related to that using Javascrit to change behaviour on queued files here: AjaxFileUpload automatically upload file once selected perhaps there is something like "Sys.Extended.UI.AjaxFileUpload.prototype._QueuedFiles" that I can use to get the filename list at the time of submitting the form.
Thanks!
PS: Save the binaries on the DB is out of the question because it means a hugue increase on database size since there will be several users each one uploading files about 6Mb each thus increasing DB size.

Keeping cached browser data inside ASP update panel textboxes/dropdowns for browser back click

I'm new in VB.net/asp and am running a VB web application in a visual database program called IronSpeed designer. I'm primarily using IronSpeed in this case for its login/role security features.
I have a basic two page setup for this app. The user logs in then is taken to AccountEntry.aspx, they enter data into textboxes and select some dropdown values that are linked to a sql database, then they click "submit" to move to Results.aspx. On Results.aspx, the user can change data and then generate several types of reports (PDF, Excel, etc).
I'm used to setting up ASP controls inside ASPContent areas, and in these areas if a user performs a browser back click the previously entered data will still be on the page for potential user modification.
However in this web app, IronSpeed is setting up the page and asp controls inside an asp update panel. It appears inside an asp update panel, cached values can't be seen on a browser back click. In this case, it's important that the orginally entered values still be there for the user experience if the user advances to Results.aspx then clicks browser back to modify a value on AccountEntry.aspx.
If I have to I'll setup Session Variables and disable browser clicking, but that is last resort. Is there any way to save cached data inside an asp update panel and have it there for a browser back click?
The best you can do is creating a class, filling it with your form data and
store it in browser session.
Public Class Navigation
Property Data1 as string
Property Data2 as string
Property ComboValue as string
Property WhateverDesiredValue as string
End Class
Save the user data into the class and keep it in Session.
Dim myNavitadionClass as new PreviousNavigation With { .Data1="" 'Assign values }
Session("PreviousNavigation") = myNavitadionClass
'Submit the form.
Then when you came again to the form you check the page_load event.
If Not Session("PreviousNavigation") is Nothing Then
Dim myClass as Navigation = Ctype(Session("PreviousNavigation"),Navigation)
End If
And then you can fill the form exactly as it was.

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.

In asp.net mvc2 how to open a new window and download a file

In my ASP.NET MVC2 app, I want to have a button in my view that collects some parameters off some form fields, then calls a controller action that returns a FileResult to stream a file (according to the parameters) to a new brower window.
I'm fine with using jquery AJAX to call the controller action, passing the paramters, but how do I get a new window to open for the streamed file?
EDIT: I know how to do stream the file back to the client browser - I'm just asking about how to write the View to collect some parameters to pass to the action.
For example, I have a fairly complex form, divided up into tabs using jquery.tabs(). On one of the tabs, the user can enter a dollar amount into a textbox, choose a template from the drop-down list then click a button to generate a voucher. So, I need to collect the values off the dollar amount box and drop-down selection (and I want to validate these, client-side) then call the controller action to generate a file to stream back to the browser.
So, my queestion is mainly a noob MVC question - what do I write in the view to specify a "mini form" that contains just those two fields, then validate them (I think I'm OK with that) then pass them to a controller action? (And I'm fine with using the link and specifying the target as _blank to open a new window.)
You usually don't call controller actions returning files using AJAX because you won't be able to do much with this file in the success callback (you cannot store the file to the client computer). So there are two possibilities: either the user is prompted with a dialog box to choose the location he wants to save the file or the contents of the file is opened inline with the default associated program (if there is any). It is the Content-Disposition header which is used to control this behavior:
Control-Disposition: attachment;filename=foo.pdf
will show a Save As dialog box to the user and it will stay on the same page. There won't be any redirect happening.
Control-Disposition: inline;filename=foo.pdf
will try to open the contents of the file inside the browser if there is an associated program and it will replace the current page.
If you want to open the contents of the file in a new browser window you could use target="_blank":
Download
Obviously this is only necessary if you are opening the file inline. For files that should be saved to the user computer you should simply provide the link and leave the user decide what to do with the file. No AJAX.
If I understand well, you must create a MemoryStream object
MemoryStream m = new MemoryStream();
Fill the object and then
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=document.doc");
HttpContext.Current.Response.BinaryWrite(m.GetBuffer());
HttpContext.Current.Response.End();
Here the list of the mime-type allowed
http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx

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