In my ASP.Net website I want to upload a file onClickEvent of LinkButton. I dont have a space to show a FileUpload Control. But if we use fileUpload control it is easy to upload a file using:
String filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/files/") + filename);
How do I open file browser on onClick event of LinkButton and save my file inside the Files folder present on the server?
EDIT:
can we use "OpenFileDialog" From Windows.Forms? If yes, how? i am just asking...
You can use javascript as suggested by Neil Thompson. However, my preferred solution to this issue is the CSS method as described in this post. You can also use the ASP upload control if you wish, and it will be hidden in the same way, allowing you to handle the file in the code behind as you usually would.
Also, as far as I can tell this works in all popular browsers.
Related
I'm Using the ASP.NET FileUpload Control for the first time and so far its working, except one thing: It seems that it is not possible to type a path and filename like
C:\Temp\myfile.txt
into the FileUpload control directly. I always have to use the OpenFileDialog.
Is there a way to enable the text box of the FileUpload control, so I can type into it?
I'm using ASP.NET 4.0.
Not that I know of. I believe the reason for this is that you could theoretically write javascript to populate it with "c:\path\to\awesome\file.txt" and upload it silently without the user's knowledge, therefore it's not possible to type stuff into it or pre-populate it in any way.
I'm developing an ASP.NET web app using VS2010,C#, I want to display a file upload control when my users click on a hyperlink (or label, it doesn't differ), and then the upload operation should be performed, I have no problem working with upload control, but currently I have an invisible upload control which display it using JavaScript in my hyperlink onclick function, the upload control is displayed but I don't know how the get the uploaded file, how should I perform this operation? I want to display upload file dialog when my users click on a label or hyperlink, then they can select their file and the file should be uploaded, and finally I should be able to work with this file in server side (I'm going to get file stream and store it in SQL), what are my options? JQuery? Ajax? JavaScript? or something else?
my JavaScript function:
function OpenFile()
{
document.getElementById("<%=fu.ClientID%>").style.display="";
var result = document.getElementById("<%=fu.ClientID%>").click();
document.getElementById("<%=fu.ClientID%>").style.display="none";
return false;
}
and my markup:
<asp:FileUpload ..... style="display:none;"....>
<ASP:hyperlink onclick="OpenFile();"...../>
After you select a file and do a PostBack, you can access the file as follows
<asp:FileUpload ID="FileUpload1" ..... style="display:none;"....>
Access the underlying posted file using the PostedFile
var fileLen = FileUpload1.PostedFile.ContentLength;
Byte[] Input = new Byte[fileLen];
myStream = FileUpload1.FileContent;
myStream.Read(Input, 0, fileLen);
Or just save it on the server:
FileUpload1.SaveAs(savePath);
I would like to recommend uploadify, it is built with flash. Very simple and easy to use with jQuery and asp.net.
Demo relies php for uploading, though it can be used with any platform including asp.net.
You have to write a handler file to do the uploading, streaming and sql storage part.
Check these answers
by #Zitun https://stackoverflow.com/a/4972872/206766
by #Blankasaurus https://stackoverflow.com/a/2501069/206766
i want to implement the simple file upload without flash.. and single file, but with jquery, there should not be any postbacks...
can anyone suggest some jquery code for uploading file to the server.
thanks in advance
Since you are only trying to upload a single file, I would use the upload control and then use a JQuery progress bar like one of the controls here:
http://dj.codeplex.com/
If you use the extender and updatepanel, there will be no postbacks.
Is it possible to limit the asp.net fileupload dialog to view only XML files?
(Filer, .xml only)
Note: Not to check whether the extension is .xml, but to actually only let the user view xml files in the dialog.
It is not possible.
Unfortunately you cannot set a filter for the file browser dialog - you have to use JavaScript or server-side validation for extension.
Upd.
You can use RegularExpression validator with the following regex string:
#"(.*?)\.(jpg|jpeg|png|gif)$"
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