File: HtmlInputFile.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (220 lines) | stat: -rw-r--r-- 7,466 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//------------------------------------------------------------------------------
// <copyright file="HtmlInputFile.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

/*
 * HtmlInputFile.cs
 *
 * Copyright (c) 2000 Microsoft Corporation
 */

namespace System.Web.UI.HtmlControls {

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Globalization;
    using System.Web;
    using System.Web.UI;
    using System.Security.Permissions;


/// <devdoc>
///    <para>
///       The <see langword='HtmlInputFile'/> class defines the
///       methods, properties, and events for the <see langword='HtmlInputFile'/> control. This class allows
///       programmatic access to the HTML &lt;input type= file&gt; element on the server.
///       It provides access to the stream, as well as a useful SaveAs functionality
///       provided by the <see cref='System.Web.UI.HtmlControls.HtmlInputFile.PostedFile'/>
///       property.
///    </para>
///    <note type="caution">
///       This class only works if the
///       HtmlForm.Enctype property is set to "multipart/form-data".
///       Also, it does not maintain its
///       state across multiple round trips between browser and server. If the user sets
///       this value after a round trip, the value is lost.
///    </note>
/// </devdoc>
    [
    ValidationProperty("Value")
    ]
    public class HtmlInputFile : HtmlInputControl, IPostBackDataHandler {

        /*
         * Creates an intrinsic Html INPUT type=file control.
         */

        /// <devdoc>
        /// <para>Initializes a new instance of the <see cref='System.Web.UI.HtmlControls.HtmlInputFile'/> class.</para>
        /// </devdoc>
        public HtmlInputFile() : base("file") {
        }

        /*
         * Accept type property.
         */

        /// <devdoc>
        ///    <para>
        ///       Gets
        ///       or sets a comma-separated list of MIME encodings that
        ///       can be used to constrain the file types that the browser lets the user
        ///       select. For example, to restrict the
        ///       selection to images, the accept value image/* should be specified.
        ///    </para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(""),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public string Accept {
            get {
                string s = Attributes["accept"];
                return((s != null) ? s : String.Empty);
            }
            set {
                Attributes["accept"] = MapStringAttributeToString(value);
            }
        }

        /*
         * The property for the maximum characters allowed.
         */

        /// <devdoc>
        ///    <para>
        ///       Gets or sets the
        ///       maximum length of the file path of the file to upload
        ///       from the client machine.
        ///    </para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(""),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public int MaxLength {
            get {
                string s = Attributes["maxlength"];
                return((s != null) ? Int32.Parse(s, CultureInfo.InvariantCulture) : -1);
            }
            set {
                Attributes["maxlength"] = MapIntegerAttributeToString(value);
            }
        }

        /*
         * PostedFile property.
         */

        /// <devdoc>
        ///    <para>Gets access to the uploaded file specified by a client.</para>
        /// </devdoc>
        [
        WebCategory("Default"),
        DefaultValue(""),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public HttpPostedFile PostedFile {
            get { return Context.Request.Files[RenderedNameAttribute];}
        }

        /*
         * The property for the width in characters.
         */

        /// <devdoc>
        ///    <para>Gets or sets the width of the file-path text box that the
        ///       browser displays when the <see cref='System.Web.UI.HtmlControls.HtmlInputFile'/>
        ///       control is used on a page.</para>
        /// </devdoc>
        [
        WebCategory("Appearance"),
        DefaultValue(-1),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public int Size {
            get {
                string s = Attributes["size"];
                return((s != null) ? Int32.Parse(s, CultureInfo.InvariantCulture) : -1);
            }
            set {
                Attributes["size"] = MapIntegerAttributeToString(value);
            }
        }

        // ASURT 122262 : The value property isn't submitted back to us when the a page
        // containing this control postsback, so required field validators are broken
        // (value would contain the empty string).  To fix this, we return the filename.

        [
        Browsable(false)
        ]
        public override string Value {
            get {
                HttpPostedFile postedFile = PostedFile;
                if (postedFile != null) {
                    return postedFile.FileName;
                }

                return String.Empty;
            }
            set {
                // Throw here because setting the value on this tag has no effect on the
                // rendering behavior and since we're always returning the posted file's
                // filename, we don't want to get into a situation where the user
                // sets a value and does not get back that value.
                throw new NotSupportedException(SR.GetString(SR.Value_Set_Not_Supported, this.GetType().Name));
            }
        }

        /*
         * Method of IPostBackDataHandler interface to process posted data.
         */

        /// <internalonly/>
        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) {
            return LoadPostData(postDataKey, postCollection);
        }


        protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
            return false;
        }

        /*
         * Method of IPostBackDataHandler interface which is invoked whenever
         * posted data for a control has changed.  RadioButton fires an
         * OnServerChange event.
         */

        /// <internalonly/>
        void IPostBackDataHandler.RaisePostDataChangedEvent() {
            RaisePostDataChangedEvent();
        }


        protected virtual void RaisePostDataChangedEvent() {
        }


        /// <devdoc>
        /// <para>Raises the <see langword='PreRender'/> event. This method uses event arguments
        ///    to pass the event data to the control.</para>
        /// </devdoc>
        protected internal override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);

            // ASURT 35328: use multipart encoding if no encoding is currently specified
            HtmlForm form = Page.Form;
            if (form != null && form.Enctype.Length == 0) {
                form.Enctype = "multipart/form-data";
            }
        }
    }
}