how to get file name form textbox to fileupload control using javascript?

Posted by Hariinakoti under JavaScript on 9/24/2012 | Points: 10 | Views : 9070 | Status : [Member] | Replies : 4
Hi,
as for my requirement i want filepath from text to fileupload control using javascript.Please any body reply to me for my forum.

Thanks & Regards
Hari



Responses

Posted by: Navadeebans on: 9/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ,

You can't get the file path, because this is browser limitatio n, and also there is no need to store "Client File Path" into server, because your are going to upload the file in the server only.

Thanks
NavaDeeban S


Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/24/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
Hari Try this

Listing 1: Add FileUpload Control


<asp:FileUpload ID="fileDocument" runat="server"></asp:FileUpload>
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click"
Text="Upload"></asp:Button>



Listing 2: OnClientClick


<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload"
Width="64px" OnClientClick="return CheckForTestFile();" />


Listing 3: JS Filter function

<script language="javascript">
//Trim the input text
function Trim(input)
{
var lre = /^\s*/;
var rre = /\s*$/;
input = input.replace(lre, "");
input = input.replace(rre, "");
return input;
}

// filter the files before Uploading for text file only
function CheckForTestFile()
{
var file = document.getElementById('<%=fileDocument.ClientID%>');
var fileName=file.value;
//Checking for file browsed or not
if (Trim(fileName) =='' )
{
alert("Please select a file to upload!!!");
file.focus();
return false;
}

//Setting the extension array for diff. type of text files
var extArray = new Array(".txt", ".doc", ".rtf", ".pdf", ".sxw", ".odt",
".stw", ".html", ".htm", ".sdw", ".vor");

//getting the file name
while (fileName.indexOf("\\") != -1)
fileName = fileName.slice(fileName.indexOf("\\") + 1);

//Getting the file extension
var ext = fileName.slice(fileName.indexOf(".")).toLowerCase();

//matching extension with our given extensions.
for (var i = 0; i < extArray.length; i++)
{
if (extArray[i] == ext)
{
return true;
}
}
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
file.focus();
return false;
}
</script>


refer this url
http://aspalliance.com/1614_Adding_Filter_Action_to_FileUpload_Control_of_ASPNET_20.all

Mark as Answer if its helpful to you

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/24/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

Please mark as answer if it helpful to you.

Mark as Answer if its helpful to you

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi jayaKumar,
fileuploaded and it is shows to another text box also. i want to that textbox value is drog&drop to fileuploader.plz solve it.

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response