Selected files to upload vanish when form is invalid on submission - asp.net

When a user selects a file to upload along with it and submits an invalid form, the file they selected disappears, while the remainder of the form remains intact. Is there a way to make it so that the selected file doesn't disappear upon an invalid submission?
I should include that I didn't make a ViewModel for this. They're two different models (the form/fields themselves, and the document info) being smushed together. Would making a ViewModel of the form with the document included in it prevent this from happening? (If so, I'd have to refactor most of my code unfortunately, which isn't the best option right now)
I was also considering throwing in unobtrusive validation via JS to bypass this, but I wanted to see some other potential solutions I could try.

Unlike other form controls, you cannot set the value for a <input type="file" /> in the controller. If you could it would be a major security issue (imagine navigating to a site that included hundreds of hidden file inputs with say "C:/Password.doc" - it would download files from your device without your knowledge).
If you need to return the view because the model is invalid, then the user needs to either re-select the file, or you need to save the file before returning the view (perhaps to a temporary location). You could then return the name and path of that file so that it could be displayed in the view to indicate that it has been uploaded (and perhaps an associated delete button so the user could delete it if they changed their mind.
Having a view model with properties for HttpPostedFileBase and the file name and its temporary path would make handling this easier.

Related

Alternative of hidden fields Asp.NET MVC

This question is more about asking suggestions over a scenario.
I have assigned a project built with MVC .NET. There are 100s of hidden fields on _Layout.cshtml page. These all hidden fields have url stored in them.
That means they all are loaded every time layout renders.
I want to optimize this situation by removing all hidden fields from layout.
To do this I am thinking of maintaining a local .JSON file which will have objectName same as id of hidden fields'.
So if a hidden field is -
<input type="hidden" id="myId" value="www.google.com"/>
Then .json goes to -
{
"myId":"www.google.com"
}
So It would be easy for me to access its value.
Is there anyway better I can do this?
instead why dont you create a singleton class and save them in that as a list and then whenever you need in view al you need is to just parse it to local javascript variable by #html.Raw(YOUSCingletonClass) and you will get all the values

ASP.NET and XML for each hit to website

I have a question to understand the concept of ASP.NET with each client browser.
I am trying to update the XML on server when a user hits a particular page on my website.
This page is dynamic, but too large so I want it to load using an XML file also I have several drop downs on the page when user changes the value in drop down, I need to refresh the data based upon the selection, additionally my drop down is a custom designed here I do not get and selectedIndex change event.
So I'm using JQuery to get the changed value in my drop down and planning to read XML from jQuery and display the data.
But since the XML is updated on hit of the page on server, I want know, if multiple users hit the same page, will the data displayed as per each users selection or it will mix the data and show the last hits record.
If I'm not mistaken about your question, you basically ask the following:
You have an XML file, which is updated, on some event from user. The page, which is displayed to the user is based on this XML file. What info will users see, when multiple users are working with the application?
This greatly depends on how you are using that file. In general, if you try to write that file to disk each time, users will see the last update. Basically update from the last user.
However, you can synchronize access to this file, write the file on per-user basis and you will see a per-user output:
<data>
<selectedData user="user name">123</selectedData>
<!-- and so on -->
</data>
UPDATE:
For anonymous users this would not work well. However, if you need to store the selection only for the current user, you can use SessionState and not an XML file.
I would like to introduce a new way that it helped me to answer my above question.
What i did here is
on Page load we added all the information related to each section in different hidden fields.
suppose i have three section with my custom drop down!
What exactly we implemented is that on click on my custom drop-down, we separated the values from the hidden field with the separator we entered and displayed the data accordingly, small part of logic but it helped in displaying the data without post back.
and Jquery helps a lot to me for just changing the inner html of my block.

Is there a way to get an httppostedfile WITHOUT using the fileuplaod control?

I have a class that needs an System.Web.HttpPostedFile object.
In most cases this is obtained when the user uploads a pic. However, in cases where they don't I need to pass a default image to the class.
But how do I populate the HttpPostedFile property if I'm not using the fileupload control?
You cannot select a file from the users machine for them. The ONLY way for a file to be selected and transferred from a client computer to your web app is if you use the input type="file" tag (which is what the file control wraps) and let THEM select a file.
What you CAN do is detect on post back whether they transferred a file to you or not. If they haven't then you can use a file local to the SERVER to use instead; however, this isn't going to come through the posted file area.
As far as reasons why not: security, plain and simple.

asp:upload postedfile lost during postback

I am using an asp:upload control to upload an image and am using the postedfile property to insert the path to the database. In my form I have a dropdown with autopostback=true where the user can select a topic to populate a checkbox list of categories. During that postback, the postedfile value is being lost and after a little research I have discovered that the posted file value is not maintained in viewstate for security reasons. Has anybody else found out how to get around this?
That's how it works. The value of a intput type="file" is never used on parsing an HTML page. That's a huge security risk, so no modern browser will allow them to 'retain' values. So in ASP.NET, every postback "loses" the value.
On any postback with a file in the input control, be sure to save the value somewhere you can get to it later.
Or don't design a form that uses a file upload to have multiple postbacks. Perhaps consider wrapping your drop down list and associated control in an UpdatePanel so the file upload doesn't get cleared.
The value attribute of an input file element simply cannot be set by anything except user interaction. That's the way it works, and that will not change due to security concerns. That said, your solution is to eliminate the postback.
I would try to load the checkbox list via JavaScript/AJAX, possibly saving the values of the checkbox list to a hidden field so I can retain the values on a postback.
This may not be ideal, but if you want to do this all with no postbacks, at some point your going to have to use JavaScript.
You could try the AsynchFileUpload control in the AjaxControlToolkit, but there isn't a workaround for the standard fileupload unless you save the file to a temporary folder and load a file list from that temp folder.

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