add.brief.option.add.gnome.vfs.binary.helper
[tfs.git] / class / Microsoft.TeamFoundation.VersionControl.Client / InternalUploadFile.cs
blobab6a4d4b68d64c26f5bd47a7b6177346b276b791
1 //
2 // Microsoft.TeamFoundation.VersionControl.Client.UploadFile
3 //
4 // Authors:
5 // Joel Reed (joelwreed@gmail.com)
6 //
7 // Copyright (C) 2007 Joel Reed
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.IO;
31 using System.Collections.Generic;
32 using System.Net;
33 using System.Reflection;
34 using System.Text;
35 using System.Xml;
36 using System.Xml.Schema;
37 using System.Web.Services;
38 using System.Web.Services.Description;
39 using System.Web.Services.Discovery;
40 using System.Web.Services.Protocols;
41 using Microsoft.TeamFoundation.VersionControl.Common;
43 namespace Microsoft.TeamFoundation.VersionControl.Client
45 internal class UploadFile
47 private HttpWebRequest request;
48 private string boundary;
49 private StreamWriter stream;
51 public UploadFile(string url, ICredentials credentials, string commandName)
53 request = (HttpWebRequest) WebRequest.Create(url);
54 request.Method = "POST";
55 request.Credentials = credentials;
56 request.AllowWriteStreamBuffering = true;
58 string sboundary = "------------" + DateTime.Now.Ticks.ToString ("x");
59 request.ContentType = String.Format ("multipart/form-data; boundary={0}", sboundary);
60 request.Headers.Add ("X-TFS-Version", "1.0.0.0");
61 request.Headers.Add ("accept-language", "en-US");
62 request.Headers.Add ("X-VersionControl-Instance",
63 String.Format("ac4d8821-8927-4f07-9acf-adbf71119886, Command{0}", commandName));
65 boundary = String.Format("--{0}\r\n", sboundary);
66 stream = new StreamWriter(request.GetRequestStream());
67 stream.Write(boundary);
70 public void AddValue(string name, string value)
72 stream.Write("Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n");
73 stream.Write(value);
75 stream.Write("\r\n");
76 stream.Write(boundary);
79 public void AddFile(string filename)
81 stream.Write("Content-Disposition: form-data; name=\"" + RepositoryConstants.ContentField + "\"; filename=\"item\"\r\n");
82 stream.Write("Content-Type: application/octet-stream\r\n\r\n");
83 stream.Flush();
85 int n = 0;
86 byte[] bytes = new byte[4096];
88 using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
90 using (BinaryReader reader = new BinaryReader(fs))
92 while ((n = reader.Read(bytes, 0, bytes.Length)) != 0) {
93 stream.BaseStream.Write(bytes, 0, n);
98 stream.Write("\r\n");
99 stream.Write(boundary);
102 public WebResponse Send()
104 stream.Close();
105 return request.GetResponse();