Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am using HTML file control and HTML image control in .aspx page (for uploading image and showing on web page).
It is working fine, but I am unable to get that uploaded image file path and file name in .aspx.cs page.

Also, I need to know how to save selected image in MS ACCESS database.

Please tell me how to solve this.

Thanks and Regards,

N.SRIRAM
Posted
Updated 4-Mar-23 3:37am
v3
Comments
Dalek Dave 6-Jan-11 3:40am    
Edited for Syntax and Readability.

Good that you got a solution but let me tell you something.

Modern browsers do not support file path. I mean you can not retrieve file path if a user is using newer browsers like IE7+, FF 3.6+, Chrome. In these browsers the file path cannot be retrieved either using JavaScript or ASP.NET server side properties of FileUpload control (if you are using one).

This feature has been included due to security reasons.

So be careful when you design you application as this may break your application.

Hope this helps!
 
Share this answer
 
Comments
Dalek Dave 6-Jan-11 3:41am    
Good Advice.
SRIRAM 2 6-Jan-11 4:51am    
hi dave ,can you send code for this? .please.

Regards,
N.SRIRAM
Ankur\m/ 6-Jan-11 4:57am    
What code? Have you not solved the issue?
try the below way..

C#
using System;
using System.IO;
class Program
{
    static void Main()
    {
        string path = "C:\\stagelist.txt";
        string extension = Path.GetExtension(path);
        string filename = Path.GetFileName(path);
        string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
        string root = Path.GetPathRoot(path);
      //  Console.WriteLine("{0}\n{1}\n{2}\n{3}",
        //    extension,
          //  filename,
            //filenameNoExtension,
            //root
);
    }

}
 
Share this answer
 
Comments
Ankur\m/ 6-Jan-11 2:40am    
Read the question again. It's related to a web page and not a console application.
Hi guys I got answer.These are helpful

XML
RETRIVING VALUES FROM JAVASCRIPT TO .ASPX.CS PAGGE
You can take following steps to read value from javascript.
1. Create html hidden field on page.
2. Set its value on client side using javascript.
3. Read html hidden field using Request.Forms["hiddenfieldname"]
You can set the variable from JavaScript into Hidden control and then you can get it in aspx page.
HTML:
<script type="text/javascript">
function abc()
{
  var str="value";
  document.getElementById("Hidden1").value=str;
}
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Hidden1" type="hidden" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClientClick="abc()"  Text="Button"
            onclick="Button1_Click" />
    </div>
    </form>
</body>
 Code Behind:
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Hidden1.Value);
    }
 But you should pay attention on the order of setting the variable to Hidden control in JavaScript and retrieving the value of Hidden control in aspx page. In the same event handle, it needs execute the Client first. For example,  OnClientClick="abc()" will be executed before onclick="Button1_Click". Otherwise, you will get the empty.
I TRIED
protected void btn(object sender, EventArgs e)
    {
        string S = txt1.Value;
        Response.Write(S);
}
ASPX:
<form id="form2" runat="server">
    <input id="File1" type="file" onchange="LoadImage()"  runat="server" />
    <img id="img" src="" alt="Image"  runat="server"  />
    <asp:Button ID="b1" Text="UPLOAD" runat="server" OnClick="btn" />
    <br />
   <input type="text" id="txt1" runat="server" />
    <br />
</form>
<script language="javascript" type="text/javascript">
               function LoadImage()
        {                  document.getElementById('img').src=document.getElementById('File1').value;    var filename=document.getElementById('File1').value;
//                for SHOWING IN A TEXTBOX
document.getElementById('txt1').value=filename;

         }
</script>
Access JavaScript variables on PostBack using ASP.NET Code
In this article, we will see how to pass javascript values on postback and then access these values in your server side code. This article will primarily showcase two techniques of doing so.  One using Hidden variables and the other using the __doPostBack() javascript method.
Using Hidden Variables – This method is pretty straightforward. All you have to do is declare a hidden field (inpHide) in your web page and then set the value of the hidden field in your JavaScript function as shown below.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Pass Javascript Variables to Server</title>
     <script type="text/javascript">
         function SetHiddenVariable()
         {
            var jsVar = "dotnetcurry.com";
            // Set the value of the hidden variable to
            // the value of the javascript variable
            var hiddenControl = '<%= inpHide.ClientID %>';
            document.getElementById(hiddenControl).value=jsVar;
         }
    </script>
</head>
<body onload="SetHiddenVariable()">
    <form id="form1" runat="server">
    <div>
        <input id="inpHide" type="hidden" runat="server" />
        <asp:TextBox ID="txtJSValue" runat="server"></asp:TextBox>
        <asp:Button ID="btnJSValue" Text="Click to retreive Javascript Variable" runat="server" onclick="btnJSValue_Click"/>
    </div>
    </form>
</body>
</html>
Then access the value of this field in the code behind on a Button click as shown below:
C#
    protected void btnJSValue_Click(object sender, EventArgs e)
    {
        txtJSValue.Text = inpHide.Value;
    }
Note: Observe that the <body> tag has an onload attribute using which the javascript function is being called.
<body onload="SetHiddenVariable()">
Using __doPostBack() – All but two ASP.NET web controls (Button & ImageButton) use the __doPostBack javascript function to cause a postback.
The EVENTTARGET is the ID of the control that caused the postback and the EVENTARGUMENT contains any arguments passed that can be accessed on the server. The __doPostBack method sets the values of the hidden fields and causes the form to be submitted to the server.
I hope this gives you a clear idea of how we can use the __doPostBack function to submit the value of a JavaScript variable to the server. All we have to do is call this JavaScript method explicitly and pass in the JavaScript variable value using the EVENTARGUMENT. Here’s an example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Pass Javascript Variables to Server</title>
     <script type="text/javascript">
         function SetHiddenVariable()
         {
            var jsVar = "dotnetcurry.com";
            __doPostBack('callPostBack', jsVar);
         }
    </script>
</head>


<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtJSValue" runat="server"></asp:TextBox>
        <asp:Button ID="btnJSValue" Text="Click to retreive Javascript Variable"
            runat="server"/>
    </div>
    </form>
</body>
</html>
The code behind will look similar to the following:
C#
protected void Page_Load(object sender, EventArgs e)
{
    this.ClientScript.GetPostBackEventReference(this, "arg");
    if (IsPostBack)
    {
        string eventTarget = this.Request["__EVENTTARGET"];
        string eventArgument = this.Request["__EVENTARGUMENT"];
        if (eventTarget != String.Empty && eventTarget == "callPostBack")
        {
            if (eventArgument != String.Empty)
                txtJSValue.Text = eventArgument;
        }
    }
    else
    {
        btnJSValue.Attributes.Add("onClick", "SetHiddenVariable();");
    }
}
The GetPostBackEventReference() emits __doPostBack() and also provides a reference to the control that initiated the postback event. The first time the page is loaded, IsPostBack is false, so we enter the ‘Else’ loop where we register the JavaScript on the button click event, using the Attribute collection of the Button control. When the user clicks the button, the JavaScript method is now called which in turn explicitly calls the __doPostBack thereby passing the JavaScript variable (jsVar) as an EVENTARGUMENT. We then access this hidden variable EVENTARGUMENT using our server side code and set the value of the textbox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900