This is a migrated thread and some comments may be shown as answers.

Uploaded File is always null!

2 Answers 338 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
W
Top achievements
Rank 1
W asked on 09 May 2008, 03:10 PM
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:

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

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

protected void Page_Load(object sender, EventArgs e) 
    { 
        // Only display the input for the upload control 
        RadUpload1.ControlObjectsVisibility = ControlObjectsVisibility.None; 
        RadUpload1.AllowedFileExtensions = new string[] { ".csv" }; 
 
        // Get QS into a string and populate label control 
        string CampaignName = Request.QueryString["CampaignName"]; 
        CampaignNameLabel.Text = CampaignName; 
    } 
 
    protected void UploadButton_Click(object sender, EventArgs e) 
    { 
        try 
        { 
            //an invalid file has tried to upload... 
            if (RadUpload1.InvalidFiles.Count > 0) 
            { 
                lblFileUploaded.Visible = true
                lblFileUploaded.ForeColor = System.Drawing.Color.Red; 
                lblFileUploaded.Text = "The file format is not valid. Please upload a CSV file."
            } 
            else 
            { 
                RadUploadContext UploadContext = RadUploadContext.Current; 
                UploadedFile File; 
                File = UploadContext.UploadedFiles[RadUpload1.UniqueID]; 
 
                File.SaveAs(Server.MapPath(@"C:\Temp\RADUpload\" + CampaignNameLabel.Text + "_Targets.csv")); 
 
                lblFileUploaded.Visible = true
                lblFileUploaded.ForeColor = System.Drawing.Color.Black; 
                lblFileUploaded.Text = "Your targets have been uploaded successfully."
            } 
 
        } 
 
        catch (Exception ex) 
        { 
 
            throw
        } 
         
    } 

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,
Brett

2 Answers, 1 is accepted

Sort by
0
Sophy
Telerik team
answered on 10 May 2008, 10:45 AM
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:
RadUpload1.UploadedFiles
or as the file input is only one the uploaded file from this file input can be accessed in the following way:
RadUpload1.UploadedFiles[0]

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,
Sophy
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Accepted
W
Top achievements
Rank 1
answered on 12 May 2008, 08:10 AM
Sophy,

Thanks for your reply.

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

Thanks again.
Tags
Upload (Obsolete)
Asked by
W
Top achievements
Rank 1
Answers by
Sophy
Telerik team
W
Top achievements
Rank 1
Share this question
or