I have 3 FileUpload controls. They lose their content when the page PostBack. The page is post back when I click on a button to show some hidden controls.
what can I do to keep their contents without using session ?
This is actually security related. Imagine visiting a site with a file upload and the path pre-filled with something like "c:\Users\Meensat\passwords.docx". You hit submit, and they have your passwords.
What you can do on postback is check for a file using the .PostedFile property, and if one exists, save it to a temp file. In Session, store a reference to the temp file, the file name, etc.
EDIT
.PostedFile is actually a property of the FileUpload control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx
That link has sample code showing how to save the file to disk.
There really isn't a way around this due to the way HTTP/Forms work. The file will get submitted on the first post to the server. You could also store the file bytes in ViewState but postbacks will be slow. Your options are to not perform a postback and use javascript or other alternatives to get the same functionality or store the file somewhere on the server (ex: session, temp folder, database).
Save the content of FileUpload control in serverside. or u can render it in the html page and keep it hidden using CSS
Related
I am having Gridivew with fileupload which has Add more rows operation.
When i Click on Add more rows the fileupload control loses the file.
I need to save all the added rows on a single submit button which should save all the files on a folder.
Kindly help me.
Thanks in advance
You cannot persist the input type="file" state after an HTTPPost. ( In ASP.NET jargon this will translate as cannot persist asp:FileUploads value after PostBack ).
Its designed that way to address security issues. There are so many examples of the same question answered here at StackOverflow ( search it ). There is a Session hack that provides a workaround but its for single file. Link here: https://stackoverflow.com/a/18656681/17447
Theoretically you could still do it using a Dictionary<int, FileUpload>. The first parameter will the GridViewRow.RowIndex and the second parameter would be the asp:FileUpload control on that row. You can start by adding all rows at the first PostBack to a session and then updating it when file.HasFile and so. But IMHO, don't do that because
Its extremely complicated.
Bad UX, it confuses the user. He wont know if there really is a file.
Clutters the ViewState immensely.
So these are the things you can do,
Prompt the user that files will be lost when they click to add the files by asking their confirmation.
Put a textbox and button asking user to pre-enter the number of files they want to add.
Use a third party plugin that use ajax to add rows.(jqGrid and datatables.net are good.)
I am working on a form on a page that uses an asp:FileUpload to allow users to upload files to a server. I'm new to ASP and am using C# for my code-behind. The plan is to have the user "attach" files one at a time, adding them to an asp:listbox. Finally when the form is submitted the files in the listbox get saved to the server.
While it seems pretty easy to save files from the FileUpload by using
myFileUpload.SaveAs("path");
I am running into some difficulty figuring out how to keep track of the files independent of the FileUpload. I can get the file names really easily using
Path.GetFileName(myFileUpload.PostedFile.FileName);
but really I need to have some way of keeping track of more than just the names. My first thought was maybe to use a temporary folder of some sort, but the files are going to potentially be pretty large so I don't want to do that because saving might take a while.
How can I keep the file around so that I can save it on the server later independent of the FileUpload?
Rather than using a ListBox I would use actual <asp:FileUpload> controls so you can have access to all of the methods for that control - such as Save Etc.
You can put a bunch of these on your page and simply hide all but the first one. Then have a button to say "Add Another" - then with the click of this button show the next <asp:FileUpload> control - JQuery would be a nice choice to show the next <asp:FileUpload> that is currently hidden.
Then in your postback you can loop through all of your <asp:FileUpload> controls and if it HasFile - which is a property on the control - then perform your saving etc.
Save them into a temporary folder if needed - perhaps renaming the file with a GUID and store this list of GUID's in the users Session so you can grab those when needed.
Once you call SaveAs, you're saving the file. The base FileUpload control won't allow you to cache the file somewhere without actually uploading it to the server first. If you are looking to upload multiple files without uploading until the end, you may need to look into dynamically generating FileUpload controls (as many as the user wants). That way they can select the files to upload one at a time, then hit an "Upload" button at the end.
It's a little clunky to do it that way, though. I'd look for some third-party multiple upload controls. I've used PLUpload in the past.
I have a form that requires a file to be uploaded as well as additional data (name, etc...) with validation on these fields. When an error occurs, the file has to be re-uploaded. Is there a way to keep the file after a validation error occurs? This is built in C# .NET.
Tried to google this but nothing helpful came up.
Thanks in advance!
You can't genually persist the file because of Security Purpose..
If page is posted and validation error occured, check for a file using the .PostedFile property, and if one exists, save it to a temp file. In Session, store a reference to the temporary file. (use javascript)
.PostedFile is a property of the FileUpload control.
Also, during my google research, I found AJAX RadControl related to your question..
http://www.telerik.com/help/aspnet-ajax/asyncupload-persist-uploaded-files.html
I'm taking a bit of a guess at what you're doing since you really can't persist a file... are you simply using a fileupload dialog box and taking it from there? If so, it's typical to put the file/path obtained from the dialog in a txt field, then do your post (upload) with a separate button.
This scenario assumes you want to persist the path/file name, but not the actual file.
The Plan
I need to upload a file without causing a full page refresh.
The uploaded files should be stored in a temporary place (session or
cookies).
I will only save the files in the server, if the user
sucessfully fills all the form fields.
Note: This is one of the slides of a jQuery slider. So a full refresh would ruin the user experience.
The Problem
If I place a Fileuploader Control inside a AJAX Update Panel, I wont be able to acess the file on the server side.
Note:From what I have found so far, this happens due to safety reasons.
Can't be done without co-operating binaries being installed on the
client. There is no safe mechanism for an AJAX framework to read the
contents of a file and therefore be able to send it to the server. The
browser supports that only as a multipart form post from a file input
box.
The Questions
When storing the files in a temporary location, should I use session or cookies? (what if the user has cookies disabled?)
If preventing a postback, really is against the standarts of user safety. Will it harm my website reputation? (regarding SEO and such)
Which road to take?
C# ASP.Net with AJAX? (is there a workarround?)
C# ASP.Net + AJAX Control Toolkit? Does it helps? (using the AsyncFileUpload control)
C# ASP.Net + jQuery Control? (won't I have problems fetching the data from the JavaScript?)
C# ASP.Net + iFrame? (not the most elegant solution)
The total amount of cookies that you can use is limited to a few kilobytes, so that's not a viable option to store a file. So sessions would be the only remaining. Consider also to save the file in the file system and remove it if it's not going to be used, as storing files in memory (session) will limit how many users you can handle at once.
No, for functions like uploading files you don't have to worry about that. Search engines doesn't try to use such functions when scanning the page.
You can use an AJAX upload in browsers that support direct file access, but there is no way around doing a post if you need to support all browsers. However, the post doesn't have to end up loading a new page, you can put a form in an iframe, or point the target of a form to an iframe.
I'm creating a website that is able to do multiple file uploads. However, is it possible such that when I click on the browse button and select the file to upload, it automatically adds it to a list of files to be uploaded, instead of selecting the file, and having another button to add that selected file to the list? It is something like uploadify.
Thanks.
I use Ajax Uploader for this
Latest build of Ajax Control Toolkit allows you to upload files without refreshing a page and without using addition button (not sure for multiuploading).
Also you may be interested in "[How Do I:] Multiple File Uploads in ASP.NET 2".