2 // System.Web.HttpPostedFile.cs
5 // Dick Porter <dick@ximian.com>
6 // Ben Maurer <benm@ximian.com>
7 // Miguel de Icaza <miguel@novell.com>
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Security.Permissions;
36 // CAS - no InheritanceDemand here as the class is sealed
37 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 public sealed class HttpPostedFile {
43 class ReadSubStream : Stream {
49 public ReadSubStream (Stream s, long offset, long length)
53 this.end = offset + length;
57 public override void Flush ()
61 public override int Read (byte [] buffer, int dest_offset, int count)
64 throw new ArgumentNullException ("buffer");
67 throw new ArgumentOutOfRangeException ("dest_offset", "< 0");
70 throw new ArgumentOutOfRangeException ("count", "< 0");
72 int len = buffer.Length;
73 if (dest_offset > len)
74 throw new ArgumentException ("destination offset is beyond array size");
75 // reordered to avoid possible integer overflow
76 if (dest_offset > len - count)
77 throw new ArgumentException ("Reading would overrun buffer");
79 if (count > end - position)
80 count = (int) (end - position);
85 s.Position = position;
86 int result = s.Read (buffer, dest_offset, count);
95 public override int ReadByte ()
100 s.Position = position;
101 int result = s.ReadByte ();
110 public override long Seek (long d, SeekOrigin origin)
114 case SeekOrigin.Begin:
120 case SeekOrigin.Current:
124 throw new ArgumentException ();
127 long virt = real - offset;
128 if (virt < 0 || virt > Length)
129 throw new ArgumentException ();
131 position = s.Seek (real, SeekOrigin.Begin);
135 public override void SetLength (long value)
137 throw new NotSupportedException ();
140 public override void Write (byte [] buffer, int offset, int count)
142 throw new NotSupportedException ();
145 public override bool CanRead {
148 public override bool CanSeek {
151 public override bool CanWrite {
152 get { return false; }
155 public override long Length {
156 get { return end - offset; }
159 public override long Position {
161 return position - offset;
165 throw new ArgumentOutOfRangeException ();
167 position = Seek (value, SeekOrigin.Begin);
172 internal HttpPostedFile (string name, string content_type, Stream base_stream, long offset, long length)
175 this.content_type = content_type;
176 this.stream = new ReadSubStream (base_stream, offset, length);
179 public string ContentType {
181 return (content_type);
185 public int ContentLength {
187 return (int)stream.Length;
191 public string FileName
198 public Stream InputStream
205 public void SaveAs (string filename)
207 byte [] buffer = new byte [16*1024];
208 long old_post = stream.Position;
211 File.Delete (filename);
212 using (FileStream fs = File.Create (filename)){
216 while ((n = stream.Read (buffer, 0, 16*1024)) != 0){
217 fs.Write (buffer, 0, n);
221 stream.Position = old_post;