Can anyone help with a small problem I am having, I have WCF Rest Based Service, which has function that can accept a stream, this will be used for uplaoding images/audio/video to the server and then storing them on the server somewhere.
Testing with and image, and it appears to work, i select the image in the client, and a few seconds later the image appears on the server in the location expected, but when i try to open the image in windows picture viewer (or any image viewer), i get “No Preview Available”, and no image to view.
I am assuming it is because i am not recreating the file again correctly from the stream.
This is the method on the 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;
}
}
And this is the client function for sending the file/image
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);
}
}
Also just tested a video file, the orignal file plays happily, then when i upload it through the service, the file that is created on the server wont play.
I am sure it is somemthing really dumb i am doing, but any help is really appreciated.
The problem was the way i was writing to the file stream, i wasnt actually passing out the bytes of the file, but rather the new bytes making the file the same size but with basically no contents of the original file.
this was the change to the code