通过邮件发送流

时间:2011-04-19 14:31:56

标签: windows-phone-7 post filestream

我正在尝试通过帖子发送照片,但我找到了一个可以执行此操作的类,但是来自FileInfo对象。我需要更改这个类才能发送一个Stream(由photoChooserTask返回),我不知道怎么做。这是班级:

public class DataContractMultiPartSerializer  
{  
  private string boundary;  
  public DataContractMultiPartSerializer(string boundary)  
  {  
      this.boundary = boundary;  
  }  

 private void WriteEntry(StreamWriter writer, string key, object value)  
 {  
     if (value != null)  
     {  
         writer.Write("--");  
         writer.WriteLine(boundary);  
         if (value is FileInfo)  
         {  

             FileInfo f = value as FileInfo;  
             writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""; ilename=""{1}""", key, f.Name);  
             writer.WriteLine("Content-Type: application/octet-stream");  
             writer.WriteLine("Content-Length: " + f.Length);  
             writer.WriteLine();  
             writer.Flush();  
             Stream output = writer.BaseStream;  
             Stream input = f.OpenRead();  
             byte[] buffer = new byte[4096];  
             for (int size = input.Read(buffer, 0, buffer.Length); size > 0; size = nput.Read(buffer, 0, buffer.Length))  
             {  
                 output.Write(buffer, 0, size);  
             }  
             output.Flush();  
             writer.WriteLine();  
         }  
         else  
         {  
             writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""", key);  
             writer.WriteLine();  
             writer.WriteLine(value.ToString());  
         }  
     }  
 }  

我试图通过这种方式改变它:

    if (value is Stream)
    {
        //FileInfo f = value as FileInfo;
        FileStream f = value as FileStream;

        writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""; filename=""{1}""", key, "sentPhoto.jpg");
        writer.WriteLine("Content-Type: application/octet-stream");
        writer.WriteLine("Content-Length: " + f.Length);
        writer.WriteLine();
        writer.Flush();
        Stream output = writer.BaseStream;
        //Stream input = f.OpenRead();
        byte[] buffer = new byte[4096];

        for (int size = input.Read(buffer, 0, buffer.Length); size > 0; size = nput.Read(buffer, 0, buffer.Length))
        {
            output.Write(buffer, 0, buffer.Length);
        }
        output.Flush();
        writer.WriteLine();
    }
    else
    {
        writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""", key);
        writer.WriteLine();
        writer.WriteLine(value.ToString());
    }

但它不会进入for循环,因为size为0。 谁能给我一个线索?

非常感谢

1 个答案:

答案 0 :(得分:0)

我已将功能转换为:

private void WriteEntry(StreamWriter writer, string key, object value)
    {
        if (value != null)
        {
            writer.Write("--");
            writer.WriteLine(boundary);
            if (value is byte[])
            {
                byte[] ba = value as byte[];

                writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""; filename=""{1}""", key, "sentPhoto.jpg");
                writer.WriteLine(@"Content-Type: application/octet-stream");
                writer.WriteLine(@"Content-Length: " + ba.Length);
                writer.WriteLine();
                writer.Flush();
                Stream output = writer.BaseStream;

                output.Write(ba, 0, ba.Length);
                output.Flush();
                writer.WriteLine();
            }
            else
            {
                writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""", key);
                writer.WriteLine();
                writer.WriteLine(value.ToString());
            }
        }
    }

现在我已经传递了一个bytearray并将其写成一个整体。做我在做Stream的事情我只会损坏图像。

我希望它有助于某人