开发者

Send Stream through post

I'm trying to send a photo through post and I've found a class that does it, but from a FileInfo object. I need to change this class in order to send a Stream (returned by the photoChooserTask) and I don't know how. Here's the class:

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

I've tried to change it this way:

    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());
    开发者_运维技巧}

but it does not enter to the for loop because size is 0. Can anyone give me a clue?

Thank you very much


I've converted the function to this:

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

Now I'm passing already a bytearray and I write it in one piece. Doing what I was doing with the Stream I only got corrupted images.

I hope it helps someone

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜