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);
}
}
}
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);
}
}
}