Prevent to upload file empty 0 byte

5 posts, 0 answers
  1. Fabio
    Fabio avatar
    10 posts
    Member since:
    Sep 2007

    Posted 07 Jul 2010 Link to this post

    Hi

    is possible to check the minimum size of uploaded file in radexplore?
    Because when i upload a txt file empty 0 byte and try to open/download with Handler.ashx method:
    <%@ WebHandler Language="C#" Class="Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles.Handler" %>   
      
    using System;   
    using System.Data;   
    using System.Configuration;   
    using System.Web;   
    using System.Text;   
    using Telerik.Web.UI;   
      
    namespace Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles   
    {   
        [RadCompressionSettings(HttpCompression = CompressionType.None)] // Disable RadCompression for this page ;   
        public class Handler : IHttpHandler   
        { 
            #region IHttpHandler Members   
      
            private HttpContext _context;   
            private HttpContext Context   
            {   
                get  
                {   
                    return _context;   
                }   
                set  
                {   
                    _context = value;   
                }   
            }   
      
      
            public void ProcessRequest(HttpContext context)   
            {   
                Context = context;   
                string filePath = context.Request.QueryString["path"];   
                filePath = context.Server.MapPath(filePath);   
      
                if (filePath == null)   
                {   
                    return;   
                }   
      
                System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);   
                System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);   
      
                byte[] bytes = new byte[streamReader.BaseStream.Length];   
      
                br.Read(bytes, 0, (int)streamReader.BaseStream.Length);   
      
                if (bytes == null )   
                {   
                    return;   
                }   
      
                streamReader.Close();   
                br.Close();   
                string extension = System.IO.Path.GetExtension(filePath);   
                string fileName = System.IO.Path.GetFileName(filePath);   
      
                if (extension == ".jpg")   
                { 
                    WriteFile(bytes, fileName, "image/jpeg jpeg jpg jpe", context.Response);   
                }   
                else if (extension == ".gif")   
                { 
                    WriteFile(bytes, fileName, "image/gif gif", context.Response);   
                } 
                else if (extension == ".png"
                { 
                    WriteFile(bytes, fileName, "image/x-png", context.Response); 
                } 
                else if (extension == ".tif"
                { 
                    WriteFile(bytes, fileName, "image/tiff", context.Response); 
                }             
                else if (extension == ".pdf")   
                { 
                    WriteFile(bytes, fileName, "application/pdf", context.Response);   
                }           
                else if (extension == ".png"
                { 
                    WriteFile(bytes, fileName, "image/x-png", context.Response); 
                } 
     
                else if (extension == ".txt"
                { 
                    WriteFile(bytes, fileName, "text/plain", context.Response); 
                } 
     
                else if (extension == ".zip"
                { 
                    WriteFile(bytes, fileName, "application/zip", context.Response); 
                }  
      
            }   
      
            /// <summary>   
            /// Sends a byte array to the client   
            /// </summary>   
            /// <param name="content">binary file content</param>   
            /// <param name="fileName">the filename to be sent to the client</param>   
            /// <param name="contentType">the file content type</param>   
            private void WriteFile(byte[] content, string fileName, string contentType, HttpResponse response)               
            { 
     
                    response.Buffer = true
                    response.Clear(); 
                    response.ContentType = contentType; 
     
                    response.AddHeader("content-disposition""attachment; filename=" + fileName); 
     
                    response.BinaryWrite(content); 
                    response.Flush(); 
                    response.End(); 
                 
                                
            }   
      
            public bool IsReusable   
            {   
                get  
                {   
                    return false;   
                }   
            } 
            #endregion   
        }   
    }     
    i receive this exeption:

    Argomento specificato non compreso nell'intervallo.
    Nome parametro: offset

    Descrizione: Eccezione non gestita durante l'esecuzione della richiesta Web corrente. Per ulteriori informazioni sull'errore e sul suo punto di origine nel codice, vedere l'analisi dello stack.

    Dettagli eccezione: System.ArgumentOutOfRangeException: Argomento specificato non compreso nell'intervallo.
    Nome parametro: offset

    Errore nel codice sorgente:

    Riga 109:                response.AddHeader("content-disposition", "attachment; filename=" + fileName);
    Riga 110:
    Riga 111: response.BinaryWrite(content);
    Riga 112: response.Flush();
    Riga 113: response.End();

    File di origine: z:\Inetpub\wwwroot\MOweb\Handler.ashx    Riga: 111


    Thanks

    Fabio
  2. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 09 Jul 2010 Link to this post

    Hi Fabio,

    In order to prevent uploading files with size 0 you can use one of the following approaches:
    1. Handling OnItemCommand server-side event and canceling it if one of the uploaded files' size is 0, e.g.:
      protected void RadFileExplorer1_ItemCommand(object sender, RadFileExplorerEventArgs e)
      {
          if (e.Command == "UploadFile")
          {
              UploadedFileCollection _uploadedFiles = (sender as RadFileExplorer).Upload.UploadedFiles;//get reference to the uploaded files
       
              foreach (UploadedFile file in _uploadedFiles)
              {
                  if (file.ContentLength == 0)
                  {
                      ScriptManager.RegisterStartupScript(Page, Page.GetType(), "KEY", "alert('Cannot upload files with size 0');", true);//popup Javascript alert
                      e.Cancel = true;//cancel the event
                      break;
                  }
              }
          }
      }
    2. Second approach is to override the StoreFile() method of the content provider, e.g.:
      protected void Page_Load(object sender, EventArgs e)
      {
          RadFileExplorer1.Configuration.ContentProviderTypeName = typeof(CustomFileBrowserProviderWithFilter).AssemblyQualifiedName;
      }
       
      public class CustomFileBrowserProviderWithFilter : FileSystemContentProvider
      {
          public CustomFileBrowserProviderWithFilter(HttpContext context, string[] searchPatterns, string[] viewPaths, string[] uploadPaths, string[] deletePaths, string selectedUrl, string selectedItemTag)
              : base(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)
          {
          }
       
          public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments)
          {
              if (file.ContentLength > 0)
                  return base.StoreFile(file, path, name, arguments);
              else
              {
                  Page _page = Context.CurrentHandler as Page;
                  ScriptManager.RegisterStartupScript(_page, _page.GetType(), "KEY", "alert('Cannot upload files with size 0');", true);
                  return string.Empty;
              }
          }
      }

    I hope this helps.

    Sincerely yours,
    Dobromir
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Fabio
    Fabio avatar
    10 posts
    Member since:
    Sep 2007

    Posted 09 Jul 2010 Link to this post

    Thanks very much, i will try the first solution.
  5. Thriveni
    Thriveni avatar
    2 posts
    Member since:
    Dec 2010

    Posted 06 Dec 2010 Link to this post

    Hi Team,

    I  have an issue with radfileexplorer() control.i have overriden getFile() and StoreFile() methods in my custom filebrowsercontent provider.and i have override the __ItemCommand event to display the custom message.
    when the event fires the default message that is Message from webpage is also firing along with the custom message which is written by me.

    could you please let me know how can I remove the default alert message.that i.e .

    ---------------------------
    Message from webpage
    ---------------------------
    A file with a name same as the target already exists!
    ---------------------------
    OK  
    ---------------------------


    please suggest to remove this alert message.
    Thanks in Advance.

    Regards,
    Thriveni
  6. Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 09 Dec 2010 Link to this post

    Hello Thriveni,

    I have answered your question in this thread.

    Regards,
    Fiko
    the Telerik team
    Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017