472,374 Members | 1,565 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,374 software developers and data experts.

HttpWebRequest file upload problem

I am trying to code a file uploader (for forum/email attachments) from the
client computer to a remote web server via the PUT method (since POST is not
allowed [405 error]). However, the upload works ONLY when the file is inside
a shared folder on my computer. If I try to upload from any other folder it
does not work. Why is this?

Reason being that whenever I upload files on other forums or websites I know
I don't need to have my folder shared on my network. I just enter a path and
do it. Am I missing a setting on the remote server? Am I coding something
wrong? Please help. Here is my code:

// Upload the file to a web server using the HttpWebRequest object
string strAttachFile = "C:\temp\blah.txt";
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/myFile.txt"); // The
server path and name of the file we are saving
BinaryReader rdr = new BinaryReader(File.OpenRead(@strAttachFile)); // The
client path and name of the file we are uploading
byte[] data = rdr.ReadBytes((int)rdr.BaseStream.Length);
myHttpWebRequest.Method = "PUT";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.SendChunked = true;
myHttpWebRequest.Timeout = 10000;
myHttpWebRequest.ContentLength = data.Length;
myHttpWebRequest.KeepAlive = true;
Stream s = myHttpWebRequest.GetRequestStream();
s.Write(data, 0, data.Length);
s.Close();
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Close();
Nov 16 '05 #1
4 14221
Hi there -

what is the exception that's being thrown? What are the permissions that the
app runs with?
If you're starting it from a web site, you might have problems with the Code
Access Security policies on the machine ...

Can you please be more specific about the enviornment you/your users are
gonna use?

Cheers,
Branimir

--
Branimir Giurov
MCSD.NET, MCDBA
www.sofiadev.org

"R Reyes" <RR****@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
I am trying to code a file uploader (for forum/email attachments) from the
client computer to a remote web server via the PUT method (since POST is
not
allowed [405 error]). However, the upload works ONLY when the file is
inside
a shared folder on my computer. If I try to upload from any other folder
it
does not work. Why is this?

Reason being that whenever I upload files on other forums or websites I
know
I don't need to have my folder shared on my network. I just enter a path
and
do it. Am I missing a setting on the remote server? Am I coding
something
wrong? Please help. Here is my code:

// Upload the file to a web server using the HttpWebRequest object
string strAttachFile = "C:\temp\blah.txt";
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/myFile.txt"); // The
server path and name of the file we are saving
BinaryReader rdr = new BinaryReader(File.OpenRead(@strAttachFile)); //
The
client path and name of the file we are uploading
byte[] data = rdr.ReadBytes((int)rdr.BaseStream.Length);
myHttpWebRequest.Method = "PUT";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.SendChunked = true;
myHttpWebRequest.Timeout = 10000;
myHttpWebRequest.ContentLength = data.Length;
myHttpWebRequest.KeepAlive = true;
Stream s = myHttpWebRequest.GetRequestStream();
s.Write(data, 0, data.Length);
s.Close();
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Close();

Nov 16 '05 #2
R Reyes wrote:
Two different web servers I have tried, here they are

Here is the first on a remote web server. I think this is probably a
permissions error and am trying to figure out exactly which ones
because read/write are granted...
File upload failed: System.Net.WebException: The remote server
returned an error: (405) Method Not Allowed.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult
asyncResult) at System.Net.HttpWebRequest.GetResponse()
at myProject.ForumSubmit.uploadFile(String strAttachFile) in
C:\Inetpub\wwwroot\myProject\ForumSubmit.aspx.cs:l ine 181
What did you try? A PUT? A POST?
Here is the second connection which is just to another folder on my
computer...I also have folder with read/write access so I am not sure
why access is denied.
File upload failed: System.UnauthorizedAccessException: Access to the
path "C:\public\tempFiles\work\docs\myDoc_.doc" is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String
msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at System.IO.File.OpenRead(String path)
at myProject.ForumSubmit.uploadFile(String strAttachFile) in
c:\inetpub\wwwroot\myProject\forumsubmit.aspx.cs:l ine 170
Does the ASP.NET process have write permission for
C:\public\tempFiles\work\docs?
I'm not sure why it's not working or why a folder needs certain
access.
Because of security? It's not a good idea to allow exposed services like a
web server or an application server to do everything on your production
server.
Whenever I try uploading files from my computer to any forum
online they don't need to see my folders as having shared access. I
just choose the file click Submit and it works fine for them. So why
not here?


What has this to do with shared folders on your system? All you need is to
understand the security implications and configure your system accordingly.

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #3
R Reyes wrote:
both actually, but the one that works is PUT. i don't think there is
a problem with the code since it works some times.
Note that in case of PUT,
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
should rather be the uploaded file's content type, not a form submit.
Actually, this content type is always wrong -- no browser performs file
uploads this way.
it only breaks
when i am switching folders from the shared folders on my computer to
the unshared ones.


Excuse my ignorance, but I'm still not sure what a shared folder really is?
A file share? Again, the key is allowing the web server process (PUT) or the
application server process (POST) to write to target directory.

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #4
both actually, but the one that works is PUT. i don't think there is a
problem with the code since it works some times. it only breaks when i am
switching folders from the shared folders on my computer to the unshared
ones. but like i said, having them shared shouldn't matter because if i'm
able to upload to different websites then it shouldn't have to be different
with my project.

Here is my code:
strAttachFile = "C:\temp\blah.txt";
string strFileName = strAttachFile;
strFileName = Path.GetFileName(strFileName);
// Upload the file to a web server using the HttpWebRequest object
// When we create the object we will provide the server path
// and name of the file we are saving
// HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://www.somelocation.com:8080/"+strFileName);
// The client path and name of the file we are uploading
BinaryReader rdr = new BinaryReader(File.OpenRead(@strAttachFile));
byte[] data = rdr.ReadBytes((int)rdr.BaseStream.Length);
myHttpWebRequest.Method = "PUT";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.SendChunked = true;
myHttpWebRequest.Timeout = 10000;
myHttpWebRequest.ContentLength = data.Length;
myHttpWebRequest.KeepAlive = true;
Stream s = myHttpWebRequest.GetRequestStream();
s.Write(data, 0, data.Length);
s.Close();
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Close();

"Joerg Jooss" wrote:
R Reyes wrote:
Two different web servers I have tried, here they are

Here is the first on a remote web server. I think this is probably a
permissions error and am trying to figure out exactly which ones
because read/write are granted...
File upload failed: System.Net.WebException: The remote server
returned an error: (405) Method Not Allowed.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult
asyncResult) at System.Net.HttpWebRequest.GetResponse()
at myProject.ForumSubmit.uploadFile(String strAttachFile) in
C:\Inetpub\wwwroot\myProject\ForumSubmit.aspx.cs:l ine 181


What did you try? A PUT? A POST?
Here is the second connection which is just to another folder on my
computer...I also have folder with read/write access so I am not sure
why access is denied.
File upload failed: System.UnauthorizedAccessException: Access to the
path "C:\public\tempFiles\work\docs\myDoc_.doc" is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, Boolean useAsync, String
msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share)
at System.IO.File.OpenRead(String path)
at myProject.ForumSubmit.uploadFile(String strAttachFile) in
c:\inetpub\wwwroot\myProject\forumsubmit.aspx.cs:l ine 170


Does the ASP.NET process have write permission for
C:\public\tempFiles\work\docs?
I'm not sure why it's not working or why a folder needs certain
access.


Because of security? It's not a good idea to allow exposed services like a
web server or an application server to do everything on your production
server.
Whenever I try uploading files from my computer to any forum
online they don't need to see my folders as having shared access. I
just choose the file click Submit and it works fine for them. So why
not here?


What has this to do with shared folders on your system? All you need is to
understand the security implications and configure your system accordingly.

Cheers,

--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #5

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

Similar topics

4
by: supster | last post by:
Hello, I am trying to use HttpWebRequest to simulate sending some POST data from a form to a PHP script. I have accomplished this using: HttpWebRequest req = (HttpWebRequest)...
8
by: Du | last post by:
I'm trying to automate the upload process to yousendit.com, but the file size doesn't add up and yousendit.com keep rejecting my upload (it accepts the upload until the very end) I don't know...
0
by: Brett | last post by:
I'd like to upload a file in vb.net vis POST using HttpWebRequest. It needs to simulate some one using a webform. The webform works fine when I manually upload through the web browser. However,...
0
by: Roland Riess | last post by:
Hi, as an absolute web and configuration beginner I am stuck with a problem and so I finally seek your help again: Development tools: ASP.NET 1.1, C#, VS 2003, Windows 2000 SP4, IIS 5.0 I...
1
by: Gert Conradie | last post by:
The following code can uplaod text files. When i upload a binary file it fail. I might be: 1) using the wrong Encoding 2) will have to System.Convert.ToBase64String the content of the binary...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
I need to programatically upload a text file to a web server using the HTTPWebRequest object within .Net 2.0. So far, I have determined that: - I need a HTTP content-type of...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
5
by: kamlesh20J | last post by:
Hello, I am trying to use HttpWebRequest to send some POST data I have accomplished this using: HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://mysite.com/index.php");...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.