473,659 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy File Value to Text Value

chunk1978
224 New Member
hi there... i'd like to know if it's possible to copy a selected file's name (value?) and insert it into a basic text field thru an onchange event handler...

here is my code:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  5. <title>Untitled Document</title>
  6.  
  7. <script type="text/javascript">
  8.  
  9. function Copy()
  10.     {
  11.     document.getElementById('textfield').value = document.getElementById('filefield').value
  12. }
  13. </script>
  14.  
  15. </head>
  16.  
  17. <body>
  18. <p>
  19.   <input type="file" name="filefield" id="filefield" onchange="Copy();">
  20. </p>
  21. <p>
  22.   <input type="text" name="textfield" id="textfield">
  23. </p>
  24. </body>
  25. </html>
  26.  
Mar 1 '07 #1
12 4040
chunk1978
224 New Member
i forgot to mention: this works fine, but what i'm trying to do is just copy the file's name, instead of copying the entire address of the file...

any ideas?
Mar 1 '07 #2
acoder
16,027 Recognized Expert Moderator MVP
Your code works absolutely fine in Firefox.
Mar 1 '07 #3
chunk1978
224 New Member
Your code works absolutely fine in Firefox.
hey acoder... yeah, i forgot to add what i was really trying to do... which is to copy the file's name only instead of the entire address of the file...

perhaps there's some insane code that could read the file, and copy only the text that's after the last "/" of the file's address? but i have zero ideas...
Mar 1 '07 #4
acoder
16,027 Recognized Expert Moderator MVP
Yes, I missed your second post.

Use the string methods, in particular, lastIndexOf and substring.
Mar 1 '07 #5
chunk1978
224 New Member
Yes, I missed your second post.

Use the string methods, in particular, lastIndexOf and substring.
thanks for the advice... but i'm finding it a little difficult wrapping my head around how i could use these to do what i'd like... would i use lastIndexOf with substring? or...

???
Mar 1 '07 #6
chunk1978
224 New Member
after searching/attempting for hours, this is the best i can come up with that makes any sense to me... but it doesn't work... :-/

Expand|Select|Wrap|Line Numbers
  1. function Copy()
  2.     {
  3.     document.getElementById('textfield').value = document.getElementById('filefield').value;
  4.     document.getElementById('textfield').value.substring(lastIndexOf('/') + 1);
  5. }
  6.  
Mar 1 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
after searching/attempting for hours, this is the best i can come up with that makes any sense to me... but it doesn't work... :-/
You're almost there, just one correction:
Expand|Select|Wrap|Line Numbers
  1. function Copy()
  2.     {
  3.     var fileVal = document.getElementById('filefield').value;
  4.     document.getElementById('textfield').value = fileVal.substring(fileVal.lastIndexOf('/') + 1);
  5. }
  6.  
You forgot the string object - lastIndexOf is a method of the String object.
Mar 1 '07 #8
chunk1978
224 New Member
You're almost there, just one correction:
Expand|Select|Wrap|Line Numbers
  1. function Copy()
  2.     {
  3.     var fileVal = document.getElementById('filefield').value;
  4.     document.getElementById('textfield').value = fileVal.substring(fileVal.lastIndexOf('/') + 1);
  5. }
  6.  
You forgot the string object - lastIndexOf is a method of the String object.
oh sweet, yeah ok it's makes more sense now... thanks once again acoder!
Mar 1 '07 #9
chunk1978
224 New Member
i would like to note (for future references) that while implementing this discussions' code in my script, i came across an unexpected problem...

this code's onChange event handler will not fire if the file field attached to the onChange handler loads as a default style="display: none;" ... therefore it was necessary to choose a different placement for the onChange handler...

strange but true ;-)
Mar 2 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4326
by: tabonni | last post by:
Hello All I am creating an ASP page. There are a list of filename and checkbox next to it. When user checked all the documents they want and click ADD TO CLIPBOARD button. All filepaths will be copied into clipboard and then they can right-click -> paste into MS Outlook as attachments. How can I use clipboardData.setData function to do that? I saw a lot of examples they are just copy and paste text.
4
32230
by: Risto Heinonen | last post by:
Hi. I have a web page that has images and text. I can carefully select one image and the on the right of the image and then copy & paste to Word. Is it possible to make javascript do the same: select the same area (divided by <div> tag) and copy to clipboard? So far I succeeded to make the script to copy the area inside <div> but it was plain html-text.
2
1616
by: jerrygarciuh | last post by:
Hi all, The following script is giving me weird problems. I have in this directory an index.php and hurricane.php. If the script gets $i = 'on' it is supposed to back up the current index into a file called normal.php and then copy hurricane.php into index.php. This should create a backup of the index and then put the hurrican alert in place.
17
5103
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have about 20 textboxes on the form). Also to ensure that the button can't get used if the cursor isn't in a textbox field. And to ensure the contents of the clipboard are "text" contents that have been cut/copied from one of the textboxes on the form. ...
3
2782
by: =?Utf-8?B?R3JlZyBN?= | last post by:
Hello, I'm running an asp.net, intranet web application using .net framework 1.1 on IIS5.1 / 6.0. Through the web application, I would like to press a button on the web page, have another window (not web-based or part of current application) brought to the front and focused (findwindow api) and have the button scrape the contents of the focused window so the contents can be parsed and entered into the web application. Ideally, I would...
6
5447
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in it--then hold the next value through the next set of nulls, and so on. See example below: I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10. Then copy down batch "7138" all the way to ID 20 and so on....
6
5077
by: Michael | last post by:
I need to copy a huge file (around 300Mb) from a mapped network drive to another. I have created a console application and used System.IO.File.Copy function. But I want to know the process of this copying work. Is there any way? I am thinking that I can get the file size, but I don't know how to get the size copied. Thanks.
10
4953
by: Jason | last post by:
I want to create a simple program with Two buttons on the form. BUTTON 1 - BACKUP PREFS this will do the following: Copy C:\Documents and Settings\%USERNAME%\Application Data\FileZilla \sitemanager.xml to this location: \\drake\pvt\%USERNAME%\FileZilla-Prefs\ BUTTON 2 - RESTORE PREFS this will do the opposite:
7
3832
by: raghudr | last post by:
<?xml version="1.0" standalone="yes" ?> - <AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2" ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance"> <Item Name="raj" VALUE="60864"/> <Item Name="rag" VALUE="60868" /> </AddressSpace> Hi all,
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.