472,805 Members | 1,225 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

I simply can't figure this out - file uploads with iframes... please help!

hi.

i have been posting this here and elsewhere a lot and can't seem to get
resolution on this problem.
i have been trying to upload files using a hidden iframe to a
asp.net/vb.net form. the problem is that
the server code can't read the httpfilecollection. the count is always
zero.

my upload form's form tag looks like this:

<form id="uploadForm" method="post" enctype="multipart/form-data"
action="progress.aspx" onsubmit="return callToServer(this.id)">

when i remove the iframe method (callToServer), i am able to read the
uploaded files on the server side, but i want to send the files via
iframe, not by posting my upload form.

the pertanant loop in my server code is below ("myfiles.Count - 1" is
always "0 - 1"):

myfiles = HttpContext.Current.Request.Files
Dim iFile As Integer
For iFile = 0 To myfiles.Count - 1
Dim postedFile As HttpPostedFile = myfiles(iFile)
[ETC]
Next iFile

please, please help.
thanks.

Jan 3 '07 #1
1 2660

Hi Milosz,

thank you for your reply and sorry for the slow response (holidays).
while there are some subtle differences between your code and what
i have, the code is similar enough for me to still be confused as to
what i am doing wrong. i am starting to think that there might be
something wrong with the code in my client-side iframe javascript
although the IE debugger seems happy.

below is my iframe method(s). It comes from the engineers at apple
computer and you can find the link and blow-by-blow explainations
here:

http://developer.apple.com/internet/...nt/iframe.html

If it is not obvious what is going wrong for me, i would REALLY
appreciate
it if somebody could look at my UploadClient.aspx and
UploadServer.aspx.vb
files? I know this is a tall call, but i am really stuck.

Thanks in advance.

the client code:

<script type="text/javascript">

var IFrameObj; // our IFrame object

function callToServer(theFormName) {

if (!document.createElement) {
return true
};

var IFrameDoc;

var URL = 'set_progress.aspx' + buildQueryString(theFormName);

if (!IFrameObj && document.createElement) { //1
// create the IFrame and assign a reference to the
// object to our global variable IFrameObj.
// this will only happen the first time
// callToServer() is called
try { //2
var tempIFrame=document.createElement('iframe');
tempIFrame.setAttribute('id','RSIFrame');
tempIFrame.style.border='0px';
tempIFrame.style.width='0px';
tempIFrame.style.height='0px';
IFrameObj = document.body.appendChild(tempIFrame);

if (document.frames) { //3
// this is for IE5 Mac, because it will only
// allow access to the document object
// of the IFrame if we access it through
// the document.frames array
IFrameObj = document.frames['RSIFrame'];
} // end 3
} // end 2
catch(exception) { // 3
// This is for IE5 PC, which does not allow dynamic creation
// and manipulation of an iframe object. Instead, we'll fake
// it up by creating our own objects.
iframeHTML='\<iframe id="RSIFrame" style="';
iframeHTML+='border:0px;';
iframeHTML+='width:0px;';
iframeHTML+='height:0px;';
iframeHTML+='"><\/iframe>';
document.body.innerHTML+=iframeHTML;
IFrameObj = new Object();
IFrameObj.document = new Object();
IFrameObj.document.location = new Object();
IFrameObj.document.location.iframe =
document.getElementById('RSIFrame');
IFrameObj.document.location.replace = function(location) { //4
alert(location);
this.iframe.src = location;
} // end 4
} // end 3
} // end 2

if ((navigator.userAgent.indexOf('Firefox') != -1) &&
(!IFrameObj.contentDocument)) {
// we have to give Firefox 1.5 a fraction of a second
// to recognize the new IFrame
setTimeout('callToServer("'+theFormName+'")',50);
return false;
}

if (navigator.userAgent.indexOf('Gecko') !=-1 &&
!IFrameObj.contentDocument) {
// we have to give NS6 a fraction of a second
// to recognize the new IFrame
setTimeout('callToServer("'+theFormName+'")',10);
return false;
}
if (IFrameObj.contentDocument) {
// For NS6
IFrameDoc = IFrameObj.contentDocument;
} else if (IFrameObj.contentWindow) {
// For IE5.5 and IE6
IFrameDoc = IFrameObj.contentWindow.document;
} else if (IFrameObj.document) {
// For IE5
IFrameDoc = IFrameObj.document;
} else {
return true;
}
IFrameDoc.location.replace=URL;
return false;

}

function buildQueryString(theFormName) {

var theDIV = document.getElementById('uploadDIV');
var theInputs = theDIV.getElementsByTagName('input');

var qs = '';

for (e=0;e<theInputs.length;e++) {

if (theInputs[e].name!='') {
qs+=(qs=='')?'?':'&'
if (theInputs[e].value != ""){
qs+=theInputs[e].name+'='+escape(theInputs[e].value)
}
//alert("the input length is: "+theInputs.length+" and this one's
name is "+theInputs[e].name);
}
}
return qs
}

</script>

Milosz Skalecki wrote:
Hi there,

I prepared a simple example to explain the concept of 'iframe upload',
Example consists of three files:
- Upload.aspx - main page with hidden iframe and some javascript
- empty.htm - an empty html file
- FileUpload.aspx - another empty file responsible for saving file on the
disk.
The concept is quite strait forward. We use iframe's document to dynamicaly
generate (via javasctipt) html form and a file upload:

-- begin Upload.aspx --
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload.aspx.cs"
Inherits="Upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<iframe name="uploader" src="empty.htm" width="0" height="0"></iframe>
<input type="button" onclick="browseForFile()" value="Upload"/>

<script language="javascript">
//<!--

function browseForFile()
{
uploader.document.write(
'<form action="FileUpload.aspx" method="post"
enctype="multipart/form-data">' +
'<input type="file" name="file" id="file"
onchange="parent.transferFile()"/>' +
'</form>');
uploader.document.close();
setTimeout('uploader.document.getElementById("file ").click();', 0);
}

function transferFile()
{
uploader.document.forms[0].submit();
}

//-->
</script>

</div>
</form>
</body>
</html>

-- end upload.aspx --

The second aspx file has no html content, it just saves incoming files:

-- beging FileUpload.aspx c# code behind --
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];

string fileName = "c:\\temp\\photos\\" + Guid.NewGuid().ToString("N") +
System.IO.Path.GetExtension(file.FileName);
file.SaveAs(fileName);
}

Response.Write("<script language=\"javascript\">");
Response.Write("alert('done!')");
Response.Write("</script>");
}
-- end FileUpload.aspx --

hope this helps

--
Milosz Skalecki
MCAD
"pbd22" wrote:
hi.

i have been posting this here and elsewhere a lot and can't seem to get
resolution on this problem.
i have been trying to upload files using a hidden iframe to a
asp.net/vb.net form. the problem is that
the server code can't read the httpfilecollection. the count is always
zero.

my upload form's form tag looks like this:

<form id="uploadForm" method="post" enctype="multipart/form-data"
action="progress.aspx" onsubmit="return callToServer(this.id)">

when i remove the iframe method (callToServer), i am able to read the
uploaded files on the server side, but i want to send the files via
iframe, not by posting my upload form.

the pertanant loop in my server code is below ("myfiles.Count - 1" is
always "0 - 1"):

myfiles = HttpContext.Current.Request.Files
Dim iFile As Integer
For iFile = 0 To myfiles.Count - 1
Dim postedFile As HttpPostedFile = myfiles(iFile)
[ETC]
Next iFile

please, please help.
thanks.
Jan 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Guadala Harry | last post by:
1. Are IFrames supported only in uplevel versions of IE? If not IE exclusively, what browsers support IFrames? 2. Are IFrames going to be supported in the future - as far as anyone knows - or...
5
by: Kikoz | last post by:
Hi all. I assume that if the user uploads a file from aspx page IIS will put all future requests to the same page from other users in a line and all of them will be waiting until this upload...
6
by: Vic Spainhower | last post by:
Hello, I am trying to do a FTP file upload which works fine on my localhost but on my ISP server it fails. I can't seem to find where I can go to find the specific cause of the failure. In both...
1
by: ali | last post by:
I am writing a script which uploads file to a specific directory; I am using javascript to handle client side exceptions and php script which actually performs file uploading. Php scripts gets...
1
by: krishan123456 | last post by:
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given...
12
by: lawrence k | last post by:
I've a form that starts off like this: <form method="post" action="profile.php? id=5&formName=my_story_edit.htm" enctype="multipart/form-data"> <p>Email address:<br /> <input type="text"...
6
Markus
by: Markus | last post by:
I'm adding to my script a section that allows a thumbnail to be created and saved. I get this error: Warning: imagejpeg() : Unable to open '../uploads/thumb/' for writing: Is a directory in...
2
by: yuvaly | last post by:
Hi I hope this is the right forum for this question... I have a form, from which I want to asynchronously upload one or more pictures. After each image is uploaded, I want to display its...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.