简体   繁体   中英

C# upload file to server - both client and server side

I'm a C# game programmer with little web development experience.

I need to upload a small file (25-100 or so bytes, depending on it's content) to a server. This is on the Windows Phone 7 using XNA. The target server is fairly limited and only supports PHP and classic ASP.

Since the CF on the WP7 only has access to a limited subset of networking commands, it's looking like an HttpWebRequest GET aimed at a script that saves the file will be the best option. The data I'm sending is small in size, and should be able to be passed as a parameter in the url.

I've been searching but have yet to find a complete example of this, which handles both the client and server side script (mainly the latter). This is close to what I'm looking for, except it has no mention of the server side script: Upload files with HTTPWebrequest (multipart/form-data)

The closest that I got was this: http://www.johny.org/2007/08/upload-using-c-as-client-and-php-as-server/

But when attempting to use it I get an unhandled exception: "The remote server returned an error: (405) Method Not Allowed". This method seems the most promising so far, but I've yet to be able to debug this.

Unfortunately, I have a short amount of time to implement this, and as I said only a passing familiarity with web development. I'm not worried about maximum security or scalability as this is a temporary measure to collect feedback internally. Basically, I just need the quickest thing that works. ;)

Any help would be fantastic!

I've solved it. First off, PHP wasn't supported on my server (just now learning that PHP and ASP are can't be used on the same server, depending on whether it's on Linux or Windows - like I said, web development noob here!). I switched to ASP and, after digging through the docs, wrote this script:

<%

dim theData, theFileName
set theData=Request("data")
set theFileName=Request("filename")

dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(Server.MapPath(theFileName+".txt"))
tfile.WriteLine(theData)
tfile.Close
set fname=nothing
set fs=nothing
set theData=nothing
set theFileName=nothing

%>

This C# code uploads the file:

        const string cAddress = "http://site.com/folder/upload.asp";
        string fileName = foo;
        string data = bar;
        string address = cAddress + "?filename=" + fileName + "&data=" + data;

        uploadRequest = (HttpWebRequest) HttpWebRequest.Create(address);
        uploadRequest.Method = "GET";
        uploadRequest.GetResponse();

Hope this helps someone else looking for an example of how to do this!

But you have the METHOD as GET instead of POST. You can't upload a file to a website by passing the file path to the Query String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM