2 // Microsoft.TeamFoundation.VersionControl.Client.UploadFile
5 // Joel Reed (joelwreed@gmail.com)
7 // Copyright (C) 2007 Joel Reed
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System
.Collections
.Generic
;
33 using System
.Reflection
;
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");
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");
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
);
99 stream
.Write(boundary
);
102 public WebResponse
Send()
105 return request
.GetResponse();