誰でも私が抱えている小さな問題を手伝ってもらえますか?ストリームを受け入れる機能を持つWCF Rest Based Serviceがあります.これは、画像/オーディオ/ビデオをサーバーにアップロードし、サーバーのどこかに保存するために使用されます.
画像でテストし、動作しているように見えます。クライアントで画像を選択すると、数秒後にサーバーの予想される場所に画像が表示されますが、Windows 画像ビューアー (または任意の画像) で画像を開こうとすると、画像ビューア)、「プレビューがありません」というメッセージが表示され、表示する画像がありません。
ストリームからファイルを正しく再作成していないためだと思います。
これは、WCF Rest Service のメソッドです。
public void PutFileInFolder(int eid, Stream fileContents)
{
try
{
byte[] buffer = new byte[32768];
MemoryStream ms = new MemoryStream();
int bytesRead = 0;
int totalBytesRead = 0;
do
{
bytesRead = fileContents.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
ms.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
//now have file in memorystream
//save the file to the users folder
FileStream file = new FileStream(@"C:\bd_sites\ttgme\wwwroot\Evidence\{" + ed.LearnerID + @"}\" + ed.EvidenceFileName, FileMode.Create, System.IO.FileAccess.Write);
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, (int)ms.Length);
file.Write(bytes, 0, bytes.Length);
file.Close();
ms.Close();
}
catch (Exception ex)
{
return;
}
}
そして、これはファイル/画像を送信するためのクライアント関数です
private void PostFile(EvidenceObject eo)
{
try
{
// Create the REST request.
string url = ConfigurationManager.AppSettings["serviceUrl"];
string requestUrl = string.Format("{0}/PutFileInFolder/{0}", 1001);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
request.ContentType = "text/plain";
byte[] fileToSend = File.ReadAllBytes(txtFileName.Text);
request.ContentLength = fileToSend.Length;
using (Stream requestStream = request.GetRequestStream())
{
// Send the file as body request.
requestStream.Write(fileToSend, 0, fileToSend.Length);
requestStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
MessageBox.Show("File sucessfully uploaded.", "Upload", MessageBoxButton.OK, MessageBoxImage.Information);
this.DialogResult = true;
}
catch (Exception ex)
{
MessageBox.Show("Error during file upload: " + ex.Message, "Upload", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
また、ビデオファイルをテストしたところ、元のファイルは問題なく再生されましたが、サービスを介してアップロードすると、サーバーで作成されたファイルが再生されません。
私がやっていることは本当にばかげていると確信していますが、どんな助けも本当に感謝しています。