FileUpload keeps uploading the same file - c#

Simple FileUpload example won't work properly on any browser. Instead what it does when the button is clicked is it keeps uploading the same file over and over again.
It seems like this only happens when the file being uploaded is saved under the project path. If I try to save the file on Desktop for example, it works fine.
HTML
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:FileUpload ID="FileUpload1" runat="server" />
</div>
</form>
C#
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = Server.MapPath("/Files/");
FileUpload1.SaveAs(path + FileUpload1.FileName);
}
}

Related

Getting value of Html textbox when html Button Clicked

I am trying to get html textbox value when click on html button without using "runat" attribute.I need to do it in code behind ,is it possible?
<button id="button1" onclick="btnclick">Click Here</button><input type="text" id="txtBox1" name="txtBox12" />
and My code Behind is like:
protected void btnclick(object sender, EventArgs e)
{
string name = Request.Form["txtBox12"];
Response.Write(name);
}
MODIFIED
To access any control in code behind it needs to have runat=server attribute attached to it.
In your case, like you pointed you can transfer the value of input text to asp hidden field and then access
its value on button click in server-side but in this case too you have to place hidden field inside form runat=server tag.
<form id="form1" runat="server">
<input type="text" id="txt" onkeyup="PassValue(this);";/>
<asp:HiddenField ID="hf" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
</form>
-----------------------
<script type="text/javascript">
function PassValue(obj) {
document.getElementById("hf").value = obj.value;
}
</script>

Change Image Url at button click

In my asp.net page I have Image control , in which myimage.png will display at page load. Requirement as below,
Browse image using File upload control and on click of Upload button , immediate preview need to be displayed in Image control. When upload button is pressed after browsing image, the existing "myimage.png" will be deleted and new image will be saved into sever path with same name and preview need to be displayed in image control.
Issue is After saving image , image control is not displaying the new image immediately.
To view the image page need to be re-loaded. Code as below,
In aspx page,
<asp:Image ID="imgLogo" style="margin-left: -299px;" ImageUrl="~/images/myimage.png" runat="server" />
Code behind as below,
protected void btnUpload_Click(object sender, EventArgs e)
{
string filePath = FileUpload1.PostedFile.FileName;
File.Delete(Server.MapPath(#"~\images\myimage.png"));
FileUpload1.SaveAs(Server.MapPath(#"~\images\myimage.png"));
imgLogo.ImageUrl = Server.MapPath(#"~\images\myimage.png");
}
Regards
The reason your page must be reloaded is because ASP.NET code is executed on a server. So every time you, for example, click the button with a server-side code inside, the request is sent to the server, server executes your code and returns a proper response. Basically: something MUST be reloaded. It doesn't have to be the entire page, though. You can force the browser to reload only a certain piece of page using AJAX extensions such as Script Manager and Update Panel.
Example (aspx page):
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Click the button."></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Hello World" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
OnClick event:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Hello world!";
}
Also, you can force the panel to update it's content using the Update() method of the UpdatePanel.
This looks like a browser caching issue.
The url is exactly the same as it was prior to changing the image file, so the cache will use the originally named file (in its cache) to display.
You can get over this hurdle by adding something extra (but not used) to the url:
protected void btnUpload_Click(object sender, EventArgs e)
{
// Do your upload stuff here...
imgLogo.ImageUrl = "~/images/myimage.png?" + DateTime.Now;
}

How to save tinymce using asp button

I'm using asp.net and c# to build a website that can upload,edit,save the file. I have use tinymce as the texteditor but the problem is the I dint know how to configure the save plugins in tinymce..I have add asp button to use as save button but how to configure it to save into file .html?
javascript to replace tiny with texeditor:
$('#<%=btnsave.ClientID%>').show() //save button
tinymce.init({selector: "textarea"}); //replace texteditor
this is markup code:
<textarea name="textarea" cols="100" rows="5" style="visibility: hidden;">
</textarea>
<br /><asp:Button Text="Save" runat="server" ID="btnsave" style="display:none;" onclick="btnsave_Click" />
This in code behind
protected void btnsave_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/WordExcelPPointToHtml/test.html");
File.WriteAllText(path,HtmlTextArea); //this line have error!!
}
Can anyone help me on this ?. Thanks in advance. Eagerly waiting for answers.
You need to replace the html control with an ASP.NET web control, and they you will be able to access the value programmatically in the code behind.
E.g.
<asp:TextBox ID="txt" Name="textarea" Columns="100" Rows="5" TextMode="MultiLine" runat="Server"></asp:TextBox>
And in the code behind:
protected void btnsave_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/WordExcelPPointToHtml/test.html");
File.WriteAllText(path, txt.Text);
}
You should also consider some error handling in btnSave_Click

File Upload Issue - how to fix?

I'm trying to upload file in tow scenarios
First:
<input id="File2" runat="server" name="name" type="file" clientidmode="Static" />
<asp:Button ID="Button4" runat="server" clientidmode="Static"
Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
This works correctly and the postedFile not null in code behind C#
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
Second:
I want to change the browse button "text" and I know already , can't do that directly so i worked around it like that:
<b>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent" >
<input id="File1" runat="server" name="name" type="file" clientidmode="Static" onchange="setHiddenValue()" style=" visibility:hidden;" />
<br />
<input id="Button2" type="button" clientidmode="Static" onclick="triggerFileUpload()" value="HTML Button" />
<br />
<asp:Button ID="Button3" runat="server" clientidmode="Static" Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
<script language="javascript">
function triggerFileUpload() {
document.getElementById("File1").click();
}
</script>
</asp:Content>
<b>
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
When I press the Button2 the fileDialog open, I select file and everything Ok.
But when I press Button3 to get file in server side c# the Request.Files[0] is null
and Found no file posted.
I want the Request.Files[0] because I want save it in database as byte
so please if u have any idea I'll appreciate it
Thank You in advance
I did the same code (second scenario) in a blank project, and this works fine. You could do the same and check it.
Probably something around this slice of code is breaking this. Check if masterpage or current page Page_Load() method have something that break the file 'postback'. You can try:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Page_Load code
}
}

how to solve postback issue file upload control's path does not exist

I am uploading files using asp.net fileupload control , whenever postback occurs due to other field's on the page, selected path in file upload control lost. I'm using multiple file upload control on page for diff. Purpose how to solve this problem? Please help with simple & suitable example dear.
string file = Path.GetFileName(UploadSalesServiceCopy.PostedFile.FileName);
string filepath2 = ConfigurationManager.AppSettings["ServerMapImgPath"].ToString();//.......local drive path via web config
string subPath = filepath2 + file;
bool IsExists = System.IO.Directory.Exists(Server.MapPath(subPath));
if (!IsExists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
if (UploadSalesServiceCopy.HasFile)
{
//UploadSalesServiceCopy.SaveAs(subPath);//PHYSICAL path
UploadSalesServiceCopy.SaveAs(Server.MapPath(subPath));//server path
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "javascript", "alert('No File Selected.')", true);
}
While clicking on Upload button ...
Your page redirect to Page_load event first , where you set page values .
For that in
page_load(..)
{
if(!IsPostBack)
{
..setpagevalues();
}
}
That main reson...you are getting null path....
Try it..
If you are using asp.net File upload control with update panel.Then the problem is
File upload control is not compatible with partial post back.
<asp:FileUpload ID="fpTest1" runat="server" />
<asp:FileUpload ID="fpTest2" runat="server" />
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnActions" runat="server" Text="Other Actions" >
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="test" runat="server" OnClick="test_Click" Text="Upload File" />
You can see that when you click other actions button file upload will have the values.
Put the Post back controls inside the Update Panel and have the file upload controls out
of the update panel.
You simply have to use fileupload with updatepanel and also put ViewStateMode="Enabled" to fileupload.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" ViewStateMode="Enabled"/>
</ContentTemplate>
</asp:UpdatePanel>

Categories

Resources