Uploaded File is always null!

Thread is closed for posting
3 posts, 1 answers
  1. W
    W avatar
    21 posts
    Member since:
    Oct 2007

    Posted 09 May 2008 Link to this post

    Hi All,

    At some point I've had this working but I've changed a few things around and I seem to have broken it!

    I have the following code in my .aspx page:

    1 <radU:RadUpload ID="RadUpload1" runat="server" MaxFileInputsCount="1" /> 
    2     <radU:RadProgressArea ID="RadProgressArea1" runat="server" Skin="Vista"
    3     </radU:RadProgressArea> 
    4     <radU:RadProgressManager ID="RadProgressManager1" runat="server" /> 
    5 <asp:Button ID="UploadButton" runat="server" Text="Upload File" OnClick="UploadButton_Click" /> 

    I have this in my code-behind (C#)

    1 protected void Page_Load(object sender, EventArgs e) 
    2     { 
    3         // Only display the input for the upload control 
    4         RadUpload1.ControlObjectsVisibility = ControlObjectsVisibility.None; 
    5         RadUpload1.AllowedFileExtensions = new string[] { ".csv" }; 
    7         // Get QS into a string and populate label control 
    8         string CampaignName = Request.QueryString["CampaignName"]; 
    9         CampaignNameLabel.Text = CampaignName; 
    10     } 
    12     protected void UploadButton_Click(object sender, EventArgs e) 
    13     { 
    14         try 
    15         { 
    16             //an invalid file has tried to upload... 
    17             if (RadUpload1.InvalidFiles.Count > 0) 
    18             { 
    19                 lblFileUploaded.Visible = true
    20                 lblFileUploaded.ForeColor = System.Drawing.Color.Red; 
    21                 lblFileUploaded.Text = "The file format is not valid. Please upload a CSV file."
    22             } 
    23             else 
    24             { 
    25                 RadUploadContext UploadContext = RadUploadContext.Current; 
    26                 UploadedFile File; 
    27                 File = UploadContext.UploadedFiles[RadUpload1.UniqueID]; 
    29                 File.SaveAs(Server.MapPath(@"C:\Temp\RADUpload\" + CampaignNameLabel.Text + "_Targets.csv")); 
    31                 lblFileUploaded.Visible = true
    32                 lblFileUploaded.ForeColor = System.Drawing.Color.Black; 
    33                 lblFileUploaded.Text = "Your targets have been uploaded successfully."
    34             } 
    36         } 
    38         catch (Exception ex) 
    39         { 
    41             throw
    42         } 
    44     } 

    The 'if' block of my code works fine but for some reason however, my 'File' variable in my else block is always null and throws the exception (which I'm not handling very gracefully at the minute!).

    If I hover over UploadContext.UploadedFiles, there is a file in the collection with the correct name, mime type etc.

    Any ideas why File is always null?

    Thanks in advance,
  2. Sophy
    Sophy avatar
    636 posts

    Posted 10 May 2008 Link to this post

    Hello Imran,

    RadUpload uses browsers' native file input control as described in the Modifying File Input Appearance help article. Controlling the native file input control's visual appearance is rather limited, so we make the actual textbox field and browse button transparent and place other "fake" textbox and button behind them. 

    The UploadContext.UploadedFiles(<id>) expects the id of the real file input which has been used for uploading the corresponding file. As you use RadUpload control with only one file input the id of the real file input for the upload control will be <clientID>+"file0" - RadUpload1.ClienID + "file0",  

    Another way to find the uploaded files which come from the upload control is by using RadUpload's UploadedFiels collection, e.g:
    or as the file input is only one the uploaded file from this file input can be accessed in the following way:

    You can take a look at the Maniputating the Uploaded Files help article for reference.

    If you need further assistance, do contact us again.

    All the best,
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Answer
    W avatar
    21 posts
    Member since:
    Oct 2007

    Posted 12 May 2008 Link to this post


    Thanks for your reply.

    Accessing the file input by RadUpload1.UploadedFiles[0] worked.

    Thanks again.
Back to Top