Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input type="file" assignment problem...

Status
Not open for further replies.

martindavey

Programmer
Jan 2, 2000
122
GB
I have 2 forms...

The source form contains the following:-
<form name=&quot;sourceform&quot; etc>
...
<input type=&quot;file&quot; name=&quot;PhotoFile&quot;>
...

The destination form contains the following:-
<form name=&quot;destform&quot; etc>
...
<input type=&quot;hidden&quot; name=&quot;PhotoFile&quot;>
...


Everything is hidden on the destination form - it has to be this way because it's populated with data from various other forms.

I have a script which roughly does the following:-

<script>
document.all.destform.PhotoFile = document.all.sourceform.PhotoFile
<script>

After this assignment the destform is submitted and the receiving page doesn't receive a file object like it would if I just submitted it from the source form.

What am I doing wrong, how do I assign it to the destform hidden file object?

I've tried .value, but that just whacks the path in as a string - no good.

Help....
 
You cannot assign a value to the form field. This is a security precaution. You'll need to get the filename as string and handle it that way. jaredn@eae.net -
 
Sure you can, if they're in the same document! Try destform.PhotoFile.value = sourceform.PhotoFile.value
You may have to precede the formnames with document. but you shouldn't need the &quot;all.&quot;
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdragon,

I tried the following (on W2K/IE5.5) and could not get it to work. Any ideas why?

<html>
<head><title></title></head>
<body>
<form>
<input type=&quot;file&quot; name=&quot;d&quot;>
</form>
<form>
<input type=&quot;file&quot; name=&quot;e&quot;>
</form>
<button onclick=&quot;document.forms[1].e.value=document.forms[0].d.value&quot;></button>
</body><html> jared@eae.net -
 
You're right about one thing: your example doesn't work. The value attribute of a FileUpload object (create by <input type=&quot;file&quot;>) is readonly. It's a security issue.

HOWEVER, martindavey wanted to copy the value of the file input field to a HIDDEN field in another form, and that works. Try this example:
Code:
<html>
<head><title></title></head>
<body>
<form name=&quot;sourceform&quot;>
<input type=&quot;file&quot; name=&quot;filename&quot;>
</form>
<form name=&quot;destform&quot;>
<input type=&quot;text&quot; name=&quot;textfield&quot;><input type=&quot;hidden&quot; name=&quot;hiddenfield&quot;>
</form>
<button onclick=&quot;document.destform.hiddenfield.value = document.sourceform.filename.value&quot;>copy value to hidden field</button> 
<button onclick=&quot;document.destform.textfield.value = document.destform.hiddenfield.value&quot;>show hidden field</button>
</body><html>
One button will copy the file name into the hidden field, and the other will coppy the value of the hidden field into a text field, so you can verify that it does indeed contain the file name.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Well, I learned something. I didn't know that file fields were readonly.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I had already tried the .value = .value approach. The trouble is, the .value property only contains the path/filename as a string, whereas the source file object contains the mime type, extension, actual data, etc.

Therefore, if I submit the destination form as it is after assigning .value, the receiving cgi script will only receive the path/filename as a string - I need to upload the file as held in the source form.

Hope this makes sense.
 
It makes sense, but it appears that you can't do what you want to do. The attributes of a FileUpload object are readonly, so you can't copy them from one form to the other.
I don't know any way around this problem.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top