UploadFile Control loses it's data on PostBacks? - c#

What's wrong! .. seems like it's a common issue .. and even if I try to persist it's value I can only keep it in anywhere like a hiddenfield but looks like there's no property that saves the whole location of the file on the local machine and even if I got it, I can't assign the value back to the FileUpload because all of it's properties are ReadOnly!
Any simple solutions please !?

this is by design - file upload is readonly because otherwise it would be a huge security risk... see http://www.w3.org/TR/html4/interact/forms.html#file-select
depending on what functionality you are trying to implement there could be other possible solutions... please elaborate.

Imagine if you can set that field programatically; what would stop a malicious developer from populating that field and accessing the user's files?
short answer is that every postback will require the user to select the file again for upload. If the postback occurs, it means the file is bad, so the user should be selecting another file anyway.
If the postback occurs because the user entered incorrect metadata for the file, while the file itself is fine, then you can just cache the file name and display it for the user again. Just put a fake, read-only textbox on top of the actual file upload textbox and display the file name back to the user. The file should have already been uploaded to the web server so no upload is necessary (i.e. the real file upload textbox can remain blank)

Related

Is it possible to insert an image filename into a fileupload when webform is opened?

I was wondering if it is possible to insert the filename of an image on the fileupload when a webform is opened.
For example, I run the program and the fileupload already have a filename of Image_Name.jpg on it to indicate that it already has a file without clicking the Choose File Button.
No, you can't do this - it is not allowed.
I mean, if we could do this, then when you come to my site to view a cute cat picture?
Then I'll go looking around on YOUR computer, and upload a file called my banking, or my passwords or whatever.
So, you can't set the file name. And in fact, EVEN when a user goes to select a file name? when they post the page (to up-load), then you ONLY get the file name. You don't even (and can't even) get the local path name.
On the other hand, might be a smartphone, and that don't even have a drive c:, and on some tablet OS, they don't even have path names similar to windows.
So, this is 100% HANDS OFF MY computer!!!
You can't set a file name, you can't choose a file name for the user. If you could do this, then not only could you mess around and grab files form my computer?
No one would ever use the internet again - since it would be too high risk and too dangerous.

Save user settings for different users in WinForms?

I want to have a save preference option in my WinForms application. The user chooses yes/no and has an option to save preference, upon which the yes/no will be remembered and a form for such a choice will not be popped upon further re running the application.
I read about going to setting and changing but how to do it for different users, since all of them would choose for diff options and I need to maintain that.
Simply using a boolean variable will not help since it will be single user specific. Any suggestions?
(1) At event close main form, you call method/action save result. See How to save data when closing event happen in WinForms?
You can save it to XML file (or text file, SQL lite, SQL Server, etc), popular way is XML file(s).
Read XML file to get saved result before processing business logic in your WinForms application at Load form event or another event type at early stage of application start-up period.
(2) You also save result at previous period in config file https://stackoverflow.com/a/453230/3728901
Properties.Settings.Default["SomeProperty"] = "Some Value";
Properties.Settings.Default.Save(); // Saves settings in application configuration file
So since i didn't want a database , I am creating a file at the client side and saving the preference there.
At run time , I will read from the file and upon that , will decide whether to send form or not .
you can use a Model and store use selected config into it then you should serilaize it and save on database for specific user when reRun the application you can get config and deserialize it to Model and use it

SaveFileDialog Save Button Event

Okay I have this scenario over here. I'm pointing a URL to users to allow them to start the download. The download button has a navigateURL with something like www.mydomainname/files/abc.mp3. So when the user clicks it, it will automatically pop up the SaveFileDialog to allow the user to choose their save destination. My question is, do we have any access to the events of button click in the SaveFileDialog? Like FileOk event?
What I want to achieve is that I want to track the number of times the file is being downloaded. Any pointer would be very much appreciated.
Track File Download hits/count in ASP.Net
Check out the answer to this question. This seems to be a more appropriate approach.
You could check to see if the FileName property is empty. If possible, it might also be worth using sever logs to monitor that if you allow hotlinking as tracking the dialog won't track every possible way to download the file.
As soon as the stream sending the file completes, add a log to the database and that would help you generate reports or do a simple SQL to get back the number of times a song is downloaded.

Writing a Dynamically built HTML to a file in ASP.NET 4.0

I have a web form which takes in user information. The value of various text boxes is used to build a html file. I write this html to a file( with specific name) and then prompt user to Save this file.This html is used for creating outlook email signatures. Currently I have this html within the application.This has been deployed to the server. I had to set write permission on this file for all users for it to work.
Are there any security risks? What happens if multiple users access this applications and write to the file at the same time.
When you say the file has "a specific name", do you mean that it is always the same name? If so, then yes, there will be problems if multiple users use this functionality at the same time. They'll be overwriting the one file and downloading each other's data. You would need to generate a unique filename each time the process runs to avoid this.
But do you actually need to save the file?
Or is your goal purely to produce some HTML for the user to download, and the way you are doing this is by writing it to a file, and then prompting them to download that file?
If you don't need to save the file, but rather just need to generate HTML and prompt the user to save, just serve it up as a normal page, and set response headers such that their browser will download it. Something along these lines:
Response.AddHeader("content-disposition", "attachment;filename=my_file.html");
From what I understand, the user fills the web form and submits. Immediately, an html file pops up for download from the server. I think this is very neat implementation of this scenario. You just need to make sure that resources are released properly in order to prevent locking of files.
When multiple users access this application, it should not break since separate files are created with a specific name (as you have mentioned). I don't know what logic has been used to create unique names. At some peculiar situation (this is purely dependent on your name calculation logic) if the calculated specific file name somehow becomes similar to an existing file, you should have code in place to replace or create a different version of the same file name. Locking could occur if you are writing captured data from web form into the same file again and again without disposing your stream/File objects . Make sure you dispose your objects after use in the code.
It would be great if you give access to the application pool of the web application to a user who has write access to that file/folder instead of giving everyone the write access. In this way, your application gets full rights to perform write operations rather than users having rights. If users have write access on the file/folder, it is very easy for anyone to peek in and do something unexpected.
Hope this helps.

Reading and writing to files

I have a few questions. Yes this is homework and I am just trying to understand it.
This is what is being asked.
• When the button “Load” is clicked, read the file specified in the textbox (txtFilePath: Absolute path not relative) and add the objects found within to the listbox
• When the user clicks the “Save” button, write the selected record to the file specified in txtFilePath (absolute path not relative) without truncating the values currently inside
Can someone please explain to me as I am still learning this. I have the button and the textbox there and the same with the save. Now with the save button will I just have the same code as you would if you just wanted to save it. But from what I am gathering there is a database so you can load the file that you saved. Or am I making this harder than what it is?
No, no database. In these instructions, record == some selected item that needs to be appended to an existing file. Just use a stream and a writer to save the file to disk and you satisfy the requirement.
No, there is no database. What you do is interact with the Windows file system (eg, the files on your hard drive). You use the classes in the System.IO namespace to load and save files.
'Absolute path' refers to the unique location of a file in the drive expressed as a rooted expression; a 'relative path' is a partial path that points to a file relative to a given location:
c:\foo\bar\baz\my files\homework.txt
..\..\homework.txt
Those are an absolute and relative paths.
I'm not sure how much detail you are looking for here, it's hard to give a complete overview of the way filesystems work. You might want to look at the basic examples in MSDN that deal with file management.
It's hard to give a detailed analysis of this subject as it is quite a wide topic.
For file interaction you must use the System.IO namespace which has classes to easily load and save files.
http://msdn.microsoft.com/en-us/library/system.io.aspx
The link above is a good reference on MSDN on how you can get started with File Management using System.IO.
Good luck!
If I understand you correctly, your question is wether or not you need to read the file a second time before saving or otherwise treat if differently than if you created a new file.
I would say "no". You have already read the content of the file into the listbox. You just need to get the edited content from the listbox (when the user is done with it) and save it to the file (overwriting whatever is there).
First of all read up on how to read and write files. Here's a good link I found:
check it out
Next what you'll want to do is put your read/write code in the Button_Click event of each button (double click on your buttons to auto create this event assuming your using Visual Studio)
You can easily retrieve the path from your text box by accessing the .text() property of your textbox
string path = myTextBox.Text;
It's been a while since I've coded anything in c# but this is pretty basic and I think it should help.
For Load:
Read the file line by line
Add each line to the ListBox Items
For Save:
Open your save file without truncating (ie append to the file)
For each item in your ListBox Items, write it to the save file

Categories