这是代码 - 上传文件到uri。 我无法上传它。有什么问题吗?
string fileToUpload = @"F:\\upload_file.txt";
FileStream rdr = new FileStream(fileToUpload, FileMode.Open);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8080/upload.txt"); //Given URI is exists
req.Method = "POST";
req.ContentLength = rdr.Length;
req.AllowWriteStreamBuffering = true;
Stream reqStream = req.GetRequestStream();
Console.WriteLine(rdr.Length);
byte[] inData = new byte[rdr.Length];
// Get data from upload file to inData
int bytesRead = rdr.Read(inData, 0, (int)rdr.Length);
// put data into request stream
reqStream.Write(inData, 0, (int)rdr.Length);
rdr.Close();
req.GetResponse();
// after uploading close stream
reqStream.Close();
答案 0 :(得分:4)
乍一看,我认为选择@
应该用来定义特定字符串的文件可能有问题。不需要转义字符。反之亦然,请在您的网络请求中使用以下字符串。
因此,您应该将其更正为@"F:\upload_file.txt";
或"F:\\upload_file.txt";