HttpWebRequest funktioniert. WebClient.UploadFile nicht
Ich dachte, ich hätte einen Weg gefunden, um meinen Code zu vereinfachenVerwenden von WebClient.UploadFile anstelle von HttpWebRequest, aber ich bekomme am Ende eine Datei auf dem Server, die ein paar Dutzend Bytes zu kurz und beschädigt ist. Irgendeine Idee, wo der Fehler liegt?
Vielen Dank
Verwendung von HttpWebRequest (funktioniert einwandfrei):
HttpWebRequest req = (HttpWebRequest)HttpWebRequest
.Create("http://" +
ConnectionManager.FileServerAddress + ":" +
ConnectionManager.FileServerPort +
"/binary/up/" + category + "/" +
Path.GetFileName(filename) + "/" + safehash);
req.Method = "POST";
req.ContentType = "binary/octet-stream";
req.AllowWriteStreamBuffering = false;
req.ContentLength = bytes.Length;
Stream reqStream = req.GetRequestStream();
int offset = 0;
while (offset < ____)
{
reqStream.Write(bytes, offset, _________);
_______
_______
_______
}
reqStream.Close();
try
{
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
}
catch (Exception e)
{
_____________
}
return safehash;
Verwenden von WebClient (beschädigte Datei auf dem Server):
var client = new WebClient();
client.Encoding = Encoding.UTF8;
client.Headers.Add(HttpRequestHeader.ContentType, "binary/octet-stream");
client.UploadFile(new Uri("http://" +
ConnectionManager.FileServerAddress + ":" +
ConnectionManager.FileServerPort +
"/binary/up/" + category + "/" +
Path.GetFileName(filename) + "/" + safehash), filename);
return safehash;
Serverseitig handelt es sich um einen WCF-Dienst:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "up/file/{fileName}/{hash}")]
void FileUpload(string fileName, string hash, Stream fileStream);