2011-04-19 242 views
0

我試圖通過文章發送照片,我發現了一個類,但它來自FileInfo對象。我需要改變這個類爲了發送一個流(由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()); 
    } 

但由於大小爲0 誰能給我一個線索它不進入for循環?

非常感謝您

回答

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,我把它寫在一塊。做我正在做的事情,我只有損壞的圖像。

我希望它可以幫助別人