通过WCF Stream上传文件,图片不可见

时间:2012-08-10 12:54:41

标签: c# wcf image rest stream

任何人都可以解决我遇到的小问题,我有WCF Rest Based Service,它具有可以接受流的功能,这将用于将图像/音频/视频上传到服务器,然后将它们存储在服务器某处。

使用和图像测试,它似乎工作,我选择客户端中的图像,几秒钟后,图像出现在服务器上预期的位置,但当我尝试在Windows图片查看器中打开图像(或任何图像查看器),我得到“无预览可用”,并且没有要查看的图像。

我假设这是因为我没有从流中再次正确地重新创建文件。

这是WCF休息服务上的方法

    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);
        } 

    }

同样只是测试了一个视频文件,orignal文件很愉快,然后当我通过服务上传时,服务器上创建的文件就不会播放。

我确信这是我真正愚蠢的事情,但真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

问题是我写入文件流的方式,我实际上没有传出文件的字节,而是新的字节使文件大小相同但基本上没有原始文件的内容。

这是对代码的更改

//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(buffer, 0, (int)ms.Length);
//file.Write(bytes, 0, bytes.Length);
//file.Close();
//ms.Close();

using (FileStream fs = File.OpenWrite(@"C:\bd_sites\ttgme\wwwroot\Evidence\{" + ed.LearnerID + @"}\" + ed.EvidenceFileName))
{
    ms.WriteTo(fs);
    fs.Close();
    ms.Close();
}