简体   繁体   English

C#将文件上传到服务器-客户端和服务器端

[英]C# upload file to server - both client and server side

I'm a C# game programmer with little web development experience. 我是一位C#游戏程序员,几乎没有网络开发经验。

I need to upload a small file (25-100 or so bytes, depending on it's content) to a server. 我需要将一个小文件(25-100个字节左右,具体取决于其内容)上传到服务器。 This is on the Windows Phone 7 using XNA. 这是使用XNA的Windows Phone 7。 The target server is fairly limited and only supports PHP and classic ASP. 目标服务器相当有限,仅支持PHP和经典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. 由于WP7上的CF仅可访问网络命令的有限子集,因此,针对保存文件的脚本的HttpWebRequest GET似乎是最佳选择。 The data I'm sending is small in size, and should be able to be passed as a parameter in the url. 我要发送的数据很小,应该可以作为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) 除了没有提及服务器端脚本外,这与我正在寻找的内容很接近: 使用HTTPWebrequest(多部分/表单数据)上传文件

The closest that I got was this: http://www.johny.org/2007/08/upload-using-c-as-client-and-php-as-server/ 我最接近的是: 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". 但是,当尝试使用它时,出现未处理的异常:“远程服务器返回错误:(405)不允许使用方法”。 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. 不幸的是,我花了很短的时间来实现这一点,而且正如我所说的那样,对Web开发只有一点点的熟悉。 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!). 首先,我的服务器上不支持PHP(现在了解到PHP和ASP不能在同一服务器上使用,这取决于它是在Linux还是Windows上-就像我说的,这里是Web开发noob!)。 I switched to ASP and, after digging through the docs, wrote this script: 我切换到ASP,在浏览了文档之后,编写了以下脚本:

<%

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: 此C#代码上传文件:

        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. 但是您将METHOD作为GET而不是POST。 You can't upload a file to a website by passing the file path to the Query String. 您不能通过将文件路径传递到查询字符串来将文件上传到网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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