httpwebrequest usage

Last post 12-22-2010, 9:40 AM by Eric. 1 replies.
Sort Posts: Previous Next
  •  12-22-2010, 7:45 AM 65500

    httpwebrequest usage

    Hi,
     
    Now I code my fileuploads in a way the file is loaded into memory and send by HTTPwebrequest to thrid party servers, often with oauth parameters. I'm interested in the capability of checking the filesize before the file is processed. Can I handle the ajax uploader control exactly the way the .NET fileupload control is used? So ca I still do my stuff as usual but have the advantage of filesize check before loading the whole file?
     
    Thx!
  •  12-22-2010, 9:40 AM 65502 in reply to 65500

    Re: httpwebrequest usage

    Dear soppie,
     
    Yes, you can check the filesize before file is uploaded, the following is example snippet: 

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        void InsertMsg(string msg)
        {
            ListBoxEvents.Items.Insert(0, msg);
            ListBoxEvents.SelectedIndex = 0;
        }
        void Uploader_FileUploaded(object sender, UploaderEventArgs args)
        {
            Uploader uploader = (Uploader)sender;
            InsertMsg("File uploaded! " + args.FileName + ", " + args.FileSize + " bytes.");

            //Copys the uploaded file to a new location.
            //args.CopyTo("c:\\temp\\"+args.FileName);
            //You can also open the uploaded file's data stream.
            //System.IO.Stream data = args.OpenStream();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Simple Upload with Progress</title>
        <link rel="stylesheet" href="demo.css" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
            <div class="content">
                <h2>
                    Simple Upload with Progress</h2>
                <p>
                    A basic sample demonstrating the use of the Upload control.</p>
                <CuteWebUI:Uploader runat="server" ID="Uploader1" InsertText="Upload File (Max 1M)"
                    OnFileUploaded="Uploader_FileUploaded">
                    <ValidateOption MaxSizeKB="1024" />
                </CuteWebUI:Uploader>
                <br />
                <br />
                <div>
                    Server Trace:
                    <br />
                    <asp:ListBox runat="server" ID="ListBoxEvents" Width="400"></asp:ListBox>
                </div>
            </div>
        </form>    
        <script type="text/javascript">
            //validate the extensions in client side   
            //this way is not safe , just for performance   
            //try to disable it to test the server validation   
            var useclientvalidation = true;
            function CuteWebUI_AjaxUploader_OnSelect(files) {
                if (useclientvalidation) {               
                    for (var i = 0; i < files.length; i++) {
                        var item = files[i];
                        var size = item.FileSize;                              
                        if (size>1024*1024) {
                            alert("cannot be uploaded!File size is too large. The maximum file size allowed is set to 1024KB!");
                            return false;
                        }
                    }
                }
            }   
        </script>  
    </body>
    </html>

    Thank you for asking
View as RSS news feed in XML