I want to show up an OpenFileDialog box in my ASP .NET web application. I am writing in c#. I want to create a user account with an image so I need to select image from my computer and save it into a database. How can I implement it.
You can do that using
<input type="file" id="fileLoader" name="files" title="Load File" ...
The usual trick is to make it invisible, and on clicking some visible artifact (styled link, image, button... whatever) simulate a click on fileLoader:
$("#fileLoader").click();
You cannot. OpenFileDialog is something for desktop applications.
you cannot use that Windows Forms class in ASP.NET, you should use the FileUpload class/control.
Or see other alternatives: Uploading Files in ASP.net without using the FileUpload server control
Almost the same as Tigran's above but I found I needed to change the JavaScript slightly to use getElementById("...") to identify the button to click(). If I just did this in HTML/CSS Tigran's code worked fine but when I used it within an .aspx file I required the change.
.aspx
<input type="file" id="fileLoader" name="files" title="Load File" />
<asp:Button ID="LoginButton" runat="server" Text="ASP click Me" onclientclick="openfileDialog()" />
JavaScript
function openfileDialog() {
document.getElementById("fileLoader").click();
}
css
#fileLoader{
display:none;}
Related
I have 2 pages. One page sends an id number that I will use to upload data from database later on.
I send this id number through the url - location += "fileUploadPage.aspx?"+ ID".
Then I need to upload files on the server side on that page. I need the form to not reload the page, as it's removing my URL extension.
I thought about using sessionStorage - but I feel like it's better in my case, as the user can have multiple tabs open for different items to upload files to.
After uploading the file to a server side - I will also need to convert it into a PDF.
I've been trying to do this for a few days and I couldn't fix it.
I managed to upload a file from a form to a server side folder, but I couldn't deny the reload of the page.
When I did deny the reload of the page the server side function did not execute. Also, I have failed to convert into PDF.
I work with aspx.net c# on serverside.
Sadly I can't share the original code as it's on a closed place, but I made a demo on my local pc:
Any suggestions? I'm new to the area of working with files-never done that before. Any suggestions on refactoring my code or how I move the ID is more than welcomed.
The input number is also a text I will need to add to my file name
after converting it to a PDF.
<form id="myForm" name="myForm" action="FilesProblemPage.aspx" runat="server" style="margin-top: 20px;">
<select id="Number" runat="server">
<option value="3">333333333</option>
<option value="2">222222222</option>
</select>
<label runat="server">
click me to choose a file
<input id="uploadFile" name="uploadFile" style="visibility: hidden" type="file" runat="server" />
</label>
<p id="ChosenFile">no file selected</p>
<asp:Button ID="uploadButton" runat="server" Text="Upload" type="button"
OnClick="uploadButton_Click" BorderStyle="None" CssClass="button" />
</form>
let makat = location.href.split("?")[1];
if (makat == 44459999) {
$("#makat").val("workssss");
$(".checkingTemp")[0].checked = true;
$(".checkingTemp")[1].checked = true;
}
$("#body_uploadFile")[0].addEventListener("change", function (e) {
console.log($("#body_uploadFile")[0].files);
if ($("#body_uploadFile")[0].files[0] != undefined)
$("#ChosenFile").text($("#body_uploadFile")[0].files[0].name);
else
$("#ChosenFile").text("no file chosen");
})
server side :
added :
using System.IO;
protected void uploadButton_Click(object sender, EventArgs e)
{
if (uploadFile.PostedFile != null && uploadFile.PostedFile.ContentLength > 0)
{
string fileName = Path.GetFileName(uploadFile.PostedFile.FileName);
string folder = Server.MapPath("~/TempFiles/");
Directory.CreateDirectory(folder);
uploadFile.PostedFile.SaveAs(Path.Combine(folder, fileName));
try
{
Response.Write("<script>alert('operation success')</script>");
}
catch
{
Response.Write("<script>alert('operation failed')</script>");
}
}
}
well, you could still use session() to pass the ID, but then on first page load (on the next page, you save that ID into ViewState. That way, it will not matter if they have multiple pages open since when they jump to the next page, then on first page load IsPostBack = false, then you transfer to ViewState.
ViewState is per web page, were as session() is global. so, pass the id via session, and FIRST thing on next page is to transfer the value to ViewState.
However, the problem with just using a simple FileUpLoad control, is they are not all that great, and if files are larger, then you don't get any kind of progress during the up-load.
For this reason, I tend to spend some REAL efforts on file uploading. (since it is a pain for developers, and often for users alike). There are a LOT of choices in this area, but I was using the AjaxToolKit in my project, and thus adopted that one.
So, users can drag + drop files, or select many files and then hit a up-load button.
the AjaxToolKit up-loader thus looks like this:
So, the user can select a bunch of files - remove them, do whatever.
Then they can hit the up-load button.
Each file uploads - with a progress bar. And then after up-loading, I display the files uploaded.
eg this:
And the other advantage of the up-loader, is there not really a file size limit - it uploads in small chunks.
So, it really depends on how fancy you want to get, but there are quite a few "up-loader" examples and even some jquery + JavaScript ones that are quite nice.
As suggested, if you not using the AjaxControl toolkit, then you could consider it (it a bit over kill - but the toolkit does have a lot of other nice features).
As noted, you might want to better use at least a asp.net FileUpload control, but it depends on how many files, how large, and what kind of UI your looking for here?
I have 2 aspx files in my project. The first.aspx page has some content on it and when I click on a button, it will launch a frame (second.aspx that only has code to show a calendar) on the same page.
Now once that calendar(second.aspx) loads on first.aspx, I want to click a link on the calendar that will .show() a hidden DIV on the first.aspx page.
How do I access code cross pages? In other words, how can I write some code in second.aspx that will affect first.aspx.
What you're asking for is not really possible. You're probably approaching it the wrong way. What you should do is turn your calendar page into a user control so that it can be used seamlessly in first.aspx.
Here is how to get started with user controls in asp.net:
After you turn it into a user control there are different approaches to getting access to the properties of the user control from your page. Here is one approach using the FindControl method.
Hope that helps.
The easiest solution would be to show and hide your div with jquery. Simple give your div a class like:
<div class="myCalendarDiv" style="display:none" />
And your Button should look like this:
<asp:Button id="myButton" OnClientClick="return ShowCalendar();" runat="server" />
<script type="text/javascript">
function ShowCalendar() {
$(".myCalendarDiv").show();
return false;
}
</script>
Another way would be instead of creating a seprate webpage for the calendar, as proposed you can use a jquery dialog, or make a usercontrol and embedd it on the same page.
(Posted on behalf of the OP).
So since I was dealing with an Iframe, I found out that you can target the parent window which would be first.aspx.
I used "window.parent.MYFUNCTION();" to call my JavaScript function on first.aspx and show the div.
I want to validate the file size before upload on IE8+
I tried
Validate File size before upload
But didn't work.
And I can't configure every IE browser to use the "ActiveXObject" (I saw that is one way for the solution).
What I should do?
Using the ASP.NET FileUpload control you cannot use code to validate the size of a file and inform the user.
If you want a better user experience, then I suggest you investigate some open source solutions like the following:
Custom HTTP module
NeatUpload is a free option.
Silverlight/Flash option
SWFUpload is a free option.
Asynchronous chunking option
RadAsyncUpload - Telerik's ASP.NET AsyncUpload is a pay option, check website for pricing.
Try this
<form enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="myFile"><br>
</form>
<script>
$('#myFile').bind('change', function() {
alert(this.files[0].size);
});
</script>
How can I create in C# a page to response to another one? So when you press the button,login form opens in a new widow(browser tab or widow), and as you login... the form automaticaly refresh the first page.
Like the Open ID form. You press the (Connect with Facebook) button it opens a new window with the login form and then it refreshes the the website where u pressed the button.
Sorry for my English!! :) & please help!
You open a new window from a button click like this:
Markup:
<asp:button type="button" id="btnLogin" runat="server" Text="Click me" OnClientClick="javascript:window.open('newPage.aspx'); OnClick="ServerSideCode" />
Or with a regular HTML button:
<input type="button" id="btnLogin" value="Click me" onclick="javascript:window.open('newPage.aspx');" />
On newPage.aspx you define a function to auto close the current form and reload the parent form. Something like:
function closeMe()
{
window.parent.location.reload();
self.close();
}
And on server-side, when you handle your button Click event, you add this line all the way at the bottom depending on whether the process was successful. Example:
if(loginSuccessful)
ScriptManager.RegisterStartupScript(this.GetType(), "whatever", "closeMe();");
Learn more about AJAX. And technically, a C# code deal with pages (it may however handle HTTP requests & replies).
you can use ( if you open a new window) the opener object.
and you can also make use of the PostBackUrl
also you can make use of form1.Action
I am working on a simple web forms application with C# (Microsoft Visual C# 2010 Express).
I have two text boxes (textBox1, richTextBox1) a button (button1) and a web browser (webBrowser1) on the form. The web browser goes to a web page when I run the program. On that page there are two input fields that I want to autofill with the click of the button1 using the text in textBox1 and richTextBox1.
You can see the code of the input fields on that web page:
<input type="text" id="subject" tabindex="4" name="subject" value="">
<textarea class="composebody" tabindex="6" name="message" id="message" rows="20" cols="80"></textarea>
I know this is very simple, but I don't have much knowledge about C#. Any ideas how I can code that?
Thanks.
You need to write this code
webBrowser1.Document.GetElementById("subject").SetAttribute("value", subject.text);
webBrowser1.Document.GetElementById("msg").SetAttribute("value",message.text );
and need to call those two lines in
DocumentCompleted event of webbrowser.
Hope it helps.
I believe you are looking for the following:
subject.value = "Your info here";
This will solve the issue for your first item but the text area is a bit more tricky. You will probably need to include some HTML item inside the text area that you can write to. I was not able to find a good way to write to the textarea item easily. If possible, I would suggest using a different control.