Webclient UploadFile "could not find file c:\..."

S

Scott Trick

I followed the instructions from MSDN for Webclient UploadFile and I get an
error: Could not find file 'C:\testfile.xls'.

If I add the file (c:\testfile.xls) to the server I do not get the error and
the file is copied from the server to the server, rather than from the web
client to the server.

Please help!!!



Here is my code:

--------------Default.aspx---------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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" method="post" runat=server>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Style="z-index: 100;
left: 22px;
position: absolute; top: 16px" Width="537px" />
<asp:Button ID="sendfileButton" runat="server"
OnClick="sendfileButton_Click" Style="z-index: 102;
left: 27px; position: absolute; top: 55px" Text="Send File"
Width="90px" />

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


--------------- default.aspx.cs ----------------------

protected void sendfileButton_Click(object sender, EventArgs e)
{
String sPath = @"C:\PricingImportTest.xls";
String sPost = "http://webservername/confirm.aspx";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();


// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);

}


----------- confirm.aspx --------------------------

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

<!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" method="post" runat="server">
<div>

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


--------- confirm.aspx.cs --------------------------

public partial class Confirm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach(string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs(@"c:\users\tmp\" + file.FileName);
}
}
}
 
G

George

I think you confusing Upload with Download...

Upload - from client to server
Download from server to client.


George.
 
S

Scott Trick

No I'm not confused I probably just didn't explain my problem good enough.

What is happening is that the UploadFile command is looking on the c drive
of the server rather than the c drive of the webclient.

In the command below sPath is supposed to be the path of a file on the web
client and sent to the web server but the command is looking on the server
for sPath???

// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);



George said:
I think you confusing Upload with Download...

Upload - from client to server
Download from server to client.


George.


Scott Trick said:
I followed the instructions from MSDN for Webclient UploadFile and I get an
error: Could not find file 'C:\testfile.xls'.

If I add the file (c:\testfile.xls) to the server I do not get the error
and
the file is copied from the server to the server, rather than from the web
client to the server.

Please help!!!



Here is my code:

--------------Default.aspx---------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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" method="post" runat=server>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Style="z-index:
100;
left: 22px;
position: absolute; top: 16px" Width="537px" />
<asp:Button ID="sendfileButton" runat="server"
OnClick="sendfileButton_Click" Style="z-index: 102;
left: 27px; position: absolute; top: 55px" Text="Send File"
Width="90px" />

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


--------------- default.aspx.cs ----------------------

protected void sendfileButton_Click(object sender, EventArgs e)
{
String sPath = @"C:\PricingImportTest.xls";
String sPost = "http://webservername/confirm.aspx";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();


// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);

}


----------- confirm.aspx --------------------------

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

<!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" method="post" runat="server">
<div>

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


--------- confirm.aspx.cs --------------------------

public partial class Confirm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach(string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs(@"c:\users\tmp\" + file.FileName);
}
}
}
 
B

bruce barker

WebClient allows a program to upload a file via http to another server. as
you are running webclient on the web server, the server is the client, and it
can only access its own files.

it is unclear what you are trying to do. you have the browser upload a file
to the server, but don't do anything with it. did you forget to save the
browsers uploaded file to disk? are you trying to send it to another server?


-- bruce (sqlwork.com)


Scott Trick said:
No I'm not confused I probably just didn't explain my problem good enough.

What is happening is that the UploadFile command is looking on the c drive
of the server rather than the c drive of the webclient.

In the command below sPath is supposed to be the path of a file on the web
client and sent to the web server but the command is looking on the server
for sPath???

// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);



George said:
I think you confusing Upload with Download...

Upload - from client to server
Download from server to client.


George.


Scott Trick said:
I followed the instructions from MSDN for Webclient UploadFile and I get an
error: Could not find file 'C:\testfile.xls'.

If I add the file (c:\testfile.xls) to the server I do not get the error
and
the file is copied from the server to the server, rather than from the web
client to the server.

Please help!!!



Here is my code:

--------------Default.aspx---------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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" method="post" runat=server>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Style="z-index:
100;
left: 22px;
position: absolute; top: 16px" Width="537px" />
<asp:Button ID="sendfileButton" runat="server"
OnClick="sendfileButton_Click" Style="z-index: 102;
left: 27px; position: absolute; top: 55px" Text="Send File"
Width="90px" />

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


--------------- default.aspx.cs ----------------------

protected void sendfileButton_Click(object sender, EventArgs e)
{
String sPath = @"C:\PricingImportTest.xls";
String sPost = "http://webservername/confirm.aspx";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();


// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);

}


----------- confirm.aspx --------------------------

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

<!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" method="post" runat="server">
<div>

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


--------- confirm.aspx.cs --------------------------

public partial class Confirm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach(string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs(@"c:\users\tmp\" + file.FileName);
}
}
}
 
G

George

Ok, then you have some messed up code...
You Default.aspx has a FileUpload control but instead of saving file when
button is pressed (event sendfileButton_Click)
you doing some weird thing.

Your confirm.aspx.cs has correct code but what is it for no clear....
since there is no FileUpload control on confirm.aspx

Do something like this.
protected void sendfileButton_Click(object sender, EventArgs e)
{
String sPath = @"C:\PricingImportTest.xls";
if (FileUpload1.HasFile)
FileUpload1.SaveAs(sPath);
}


George


Scott Trick said:
No I'm not confused I probably just didn't explain my problem good enough.

What is happening is that the UploadFile command is looking on the c drive
of the server rather than the c drive of the webclient.

In the command below sPath is supposed to be the path of a file on the web
client and sent to the web server but the command is looking on the server
for sPath???

// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);



George said:
I think you confusing Upload with Download...

Upload - from client to server
Download from server to client.


George.


Scott Trick said:
I followed the instructions from MSDN for Webclient UploadFile and I get
an
error: Could not find file 'C:\testfile.xls'.

If I add the file (c:\testfile.xls) to the server I do not get the
error
and
the file is copied from the server to the server, rather than from the
web
client to the server.

Please help!!!



Here is my code:

--------------Default.aspx---------

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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" method="post" runat=server>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Style="z-index:
100;
left: 22px;
position: absolute; top: 16px" Width="537px" />
<asp:Button ID="sendfileButton" runat="server"
OnClick="sendfileButton_Click" Style="z-index: 102;
left: 27px; position: absolute; top: 55px" Text="Send File"
Width="90px" />

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


--------------- default.aspx.cs ----------------------

protected void sendfileButton_Click(object sender, EventArgs e)
{
String sPath = @"C:\PricingImportTest.xls";
String sPost = "http://webservername/confirm.aspx";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();


// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);

}


----------- confirm.aspx --------------------------

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

<!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" method="post" runat="server">
<div>

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


--------- confirm.aspx.cs --------------------------

public partial class Confirm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach(string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs(@"c:\users\tmp\" + file.FileName);
}
}
}
 
S

Scott Trick

What I am trying to do is allow for users to upload files to our web server.

bruce barker said:
WebClient allows a program to upload a file via http to another server. as
you are running webclient on the web server, the server is the client, and it
can only access its own files.

it is unclear what you are trying to do. you have the browser upload a file
to the server, but don't do anything with it. did you forget to save the
browsers uploaded file to disk? are you trying to send it to another server?


-- bruce (sqlwork.com)


Scott Trick said:
No I'm not confused I probably just didn't explain my problem good enough.

What is happening is that the UploadFile command is looking on the c drive
of the server rather than the c drive of the webclient.

In the command below sPath is supposed to be the path of a file on the web
client and sent to the web server but the command is looking on the server
for sPath???

// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);



George said:
I think you confusing Upload with Download...

Upload - from client to server
Download from server to client.


George.


I followed the instructions from MSDN for Webclient UploadFile and I get an
error: Could not find file 'C:\testfile.xls'.

If I add the file (c:\testfile.xls) to the server I do not get the error
and
the file is copied from the server to the server, rather than from the web
client to the server.

Please help!!!



Here is my code:

--------------Default.aspx---------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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" method="post" runat=server>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Style="z-index:
100;
left: 22px;
position: absolute; top: 16px" Width="537px" />
<asp:Button ID="sendfileButton" runat="server"
OnClick="sendfileButton_Click" Style="z-index: 102;
left: 27px; position: absolute; top: 55px" Text="Send File"
Width="90px" />

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


--------------- default.aspx.cs ----------------------

protected void sendfileButton_Click(object sender, EventArgs e)
{
String sPath = @"C:\PricingImportTest.xls";
String sPost = "http://webservername/confirm.aspx";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();


// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);

}


----------- confirm.aspx --------------------------

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

<!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" method="post" runat="server">
<div>

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


--------- confirm.aspx.cs --------------------------

public partial class Confirm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach(string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs(@"c:\users\tmp\" + file.FileName);
}
}
}
 
B

bruce barker

then instead of calling another webserver, write the uploaded file out to disk.

if (FileUpload1.HasFile)
FileUpload1.SaveAs("filename");

be sure to pick a folder the asp.net process has access to (say app_data).
don't write to the website (except app_data) or you will force a recycle.

-- bruce (sqlwork.com)


Scott Trick said:
What I am trying to do is allow for users to upload files to our web server.

bruce barker said:
WebClient allows a program to upload a file via http to another server. as
you are running webclient on the web server, the server is the client, and it
can only access its own files.

it is unclear what you are trying to do. you have the browser upload a file
to the server, but don't do anything with it. did you forget to save the
browsers uploaded file to disk? are you trying to send it to another server?


-- bruce (sqlwork.com)


Scott Trick said:
No I'm not confused I probably just didn't explain my problem good enough.

What is happening is that the UploadFile command is looking on the c drive
of the server rather than the c drive of the webclient.

In the command below sPath is supposed to be the path of a file on the web
client and sent to the web server but the command is looking on the server
for sPath???

// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);



:

I think you confusing Upload with Download...

Upload - from client to server
Download from server to client.


George.


I followed the instructions from MSDN for Webclient UploadFile and I get an
error: Could not find file 'C:\testfile.xls'.

If I add the file (c:\testfile.xls) to the server I do not get the error
and
the file is copied from the server to the server, rather than from the web
client to the server.

Please help!!!



Here is my code:

--------------Default.aspx---------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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" method="post" runat=server>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" Style="z-index:
100;
left: 22px;
position: absolute; top: 16px" Width="537px" />
<asp:Button ID="sendfileButton" runat="server"
OnClick="sendfileButton_Click" Style="z-index: 102;
left: 27px; position: absolute; top: 55px" Text="Send File"
Width="90px" />

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


--------------- default.aspx.cs ----------------------

protected void sendfileButton_Click(object sender, EventArgs e)
{
String sPath = @"C:\PricingImportTest.xls";
String sPost = "http://webservername/confirm.aspx";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();


// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(sPost, sPath);

}


----------- confirm.aspx --------------------------

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

<!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" method="post" runat="server">
<div>

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


--------- confirm.aspx.cs --------------------------

public partial class Confirm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach(string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs(@"c:\users\tmp\" + file.FileName);
}
}
}
 
B

Ben Messenger

I've got the exact same issue. Was this ever solved? What I can't understand is why a webclient object only looks for a file on the sever. It seems to defeat the purpose of a webcleint and also an uploadfile function. (You can just use a copy file instead).



brucebarke wrote:

Re: Webclient UploadFile "could not find file c:\..."
31-Oct-08

then instead of calling another webserver, write the uploaded file out to disk.

if (FileUpload1.HasFile)
FileUpload1.SaveAs("filename");

be sure to pick a folder the asp.net process has access to (say app_data).
don't write to the website (except app_data) or you will force a recycle.

-- bruce (sqlwork.com)


:

EggHeadCafe - Software Developer Portal of Choice
Access Web service in client using WSDL file
http://www.eggheadcafe.com/tutorial...f-91215d5b0b81/access-web-service-in-cli.aspx
 
P

Patrice

I've got the exact same issue. Was this ever solved? What I can't
understand is why a webclient object only looks for a file on the sever.
It seems to defeat the purpose of a webcleint and also an uploadfile
function. (You can just use a copy file instead).

I'm not sure what is the issue is. WebClient.UploadFile is to take a local
file and to send him to a web server. On the server you can only save the
file content where the server has access.

If you are not using a web server just copy the file (a source of confusion
I've seen several times is when the dev machine is both a client and the
server so if the developper doesn't understand well each of those roles, the
application doesn't work anymore in production)....
 
M

Martin M

Try XUploader from www.springsys.com


I'm not sure what is the issue is. WebClient.UploadFile is to take a local
file and to send him to a web server. On the server you can only save the
file content where the server has access.

If you are not using a web server just copy the file (a source of
confusion I've seen several times is when the dev machine is both a client
and the server so if the developper doesn't understand well each of those
roles, the application doesn't work anymore in production)....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top