我需要在服务器上上传文件(具有不同的扩展名,即.txt,.jpg,.pdf ....)。我创建了一个接受HTTP的站点,请求将虚拟目录映射到物理目录。所有THI在嫁妆中都可以正常工作,现在我必须实现上传。
这是我的代码
private void UploadFile(string uploadFileName, string localFileName)
{
//long length = 0;
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uploadFileName);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials = new NetworkCredential("USER", "PASSWORD", "DOMAIN");
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("rn--" +boundary + "rn");
string formdataTemplate = "rn--" + boundary +"rnContent-Disposition: form-data; name="{0}";rnrn{1}";
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name="{0}"; filename="{1}"rn Content-Type: application/octet-streamrnrn";
string header = string.Format(headerTemplate, "uplTheFile", localFileName);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(localFileName, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
//error returned in lenght field: "This stream does not support seek operations."
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
WebResponse webResponse2 = httpWebRequest2.GetResponse();
//error returned from getResponse: "The remote server returned an error: (405) Method Not Allowed."
//I guess cause my folder is in read only
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
MessageBox.Show(reader2.ReadToEnd());
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
}
首先,我得到了此错误:远程服务器返回了一个错误:(405)不允许的方法。因此,我尝试通过在网站上添加映射来启用帖子。
现在,在TE服务器上的文件夹中,我有一个web.config文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<handlers>
<add name="File TXT" path="*.*" verb="*" modules="IsapiModule" scriptProcessor="C:WindowsSystem32inetsrvasp.dll" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64" />
</handlers>
</system.webServer>
</configuration>
以这种方式,我不会遇到任何错误,但是该应用程序不会上传任何文件。我如何解决这个问题?
一个简单得多的解决方案是使用system.net.webclient类上传文件。
System.Net.WebClient client = new System.Net.WebClient();
client.UploadFile("www.myfileuploadendpoint.com",@"c:myfiletoupload.txt");
客户端还具有一个Onuploadfilecomplet的事件,该事件将在完成后调用处理程序。
client.OnUploadFileCompleted += UploadCompleted(sender,args) => { //do whatever you want}
这将节省您的代码和错误。祝你好运!:)
上述两个样本不起作用,
- "远程服务器返回了一个错误:(405)不允许的方法。"是尝试以上两个代码而被发现的例外
希望有人在这些例外分享他们的想法。
rgds,thiru