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

Restrict files with no extension

4 Answers 167 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
WAMatt
Top achievements
Rank 2
WAMatt asked on 14 Sep 2012, 01:36 PM
Greetings,

I would like to allow users to upload any type of file except ones with no extension.  Any ideas how to do this?

Your help is appreciated!

Matt

4 Answers, 1 is accepted

Sort by
0
Accepted
Plamen
Telerik team
answered on 18 Sep 2012, 11:36 AM
Hello Matt,

 
One way to achieve such validation is to check the uploading file's name in the OnClientFileUploading event as in the code below:

function OnClientFileUploading(sender, args) {
                var file = args.get_fileName();
                var extension = file.substr((file.lastIndexOf('.') + 1));
                if (extension == file) {
                    args.set_cancel(true);
                };
            }

Hope this will help you solve the issue.

Kind regards,
Plamen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
WAMatt
Top achievements
Rank 2
answered on 18 Sep 2012, 02:45 PM
Thank you, that got me part-way there.  I am still unsure how to handle the file to prevent the upload.  Here is what I am doing...

JavaScript...
<script type="text/javascript">
  function uplFileUpload_OnClientFileUploading(sender, args) {
    var file = args.get_fileName(); var
    extension = file.substr((file.lastIndexOf('.') + 1));
    if (extension == file) {
      args.set_cancel(true);           
    }
  }
</script>

ASPX...
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr class="pulldown_fields">
    <td width="30%" style="font-weight: bold" align="right">
      Upload Files
    </td>
    <td width="3%">
        
    </td>
    <td>
      <telerik:RadAsyncUpload ID="uplFileUpload" runat="server" UploadedFilesRendering="BelowFileInput"
        MultipleFileSelection="Automatic" OnClientFileUploading="uplFileUpload_OnClientFileUploading">
      </telerik:RadAsyncUpload>
      <br />
      <telerik:RadButton ID="btnFileUpload" runat="server" Text="Save Files" Icon-PrimaryIconCssClass="rbSave"
        OnClick="btnFileUpload_Click">
      </telerik:RadButton>
      <asp:Label ID="errFileUpload" runat="server" ForeColor="Red" />
    </td>
  </tr>
</table>

CP...
protected void btnFileUpload_Click(object sender, EventArgs e)
{
  saveAttachments(1000);
}
private void saveAttachments(int folderNumber)
{
  resetValidation();
  errFileUpload.Text = "";
  if (!Directory.Exists(Server.MapPath("~/Uploads/" + folderNumber.ToString())))
  {
    Directory.CreateDirectory(Server.MapPath("~/Uploads/" + folderNumber.ToString()));
  }
  if (uplFileUpload.UploadedFiles.Count > 0)
  {
    try
    {
      foreach (UploadedFile file in uplFileUpload.UploadedFiles)
      {
        file.SaveAs(Server.MapPath("~/Uploads/" + folderNumber.ToString() + "/" + file.GetName()), true);
        saveChangeLog("DeceasedMember",
                  Convert.ToInt32(hdnDeceasedMemberID.Value),
                  file.GetName() + " has been uploaded");
      }
      errFileUpload.Text = uplFileUpload.UploadedFiles.Count.ToString() + " file(s) were uploaded";
    }
    catch (Exception)
    {
      errFileUpload.Text = "Unable to upload file(s)";
    }
  }
  else
  {
    errFileUpload.Text = "Select a file first";
  }
}

It flags the file in the uploaded files list with a red dot, but it still allows it to be uploaded when I click the Save button.  What am I missing?

Thanks again!

Matt
0
Plamen
Telerik team
answered on 21 Sep 2012, 10:19 AM
Hi Matt,

 
This issue is a known limitation observed only in the Silverlight Upload module and if you want to avoid it you can add the following javascript to the page:

Telerik.Web.UI.RadAsyncUpload.Modules.Silverlight.isAvailable = function() { return false; }

Hope this will help you solve the issue.

Greetings,
Plamen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
WAMatt
Top achievements
Rank 2
answered on 24 Sep 2012, 02:47 PM
This gives me an error: "Telerik is not defined".  I went ahead and dealt with this using server-side code...
private void saveAttachments(int folderNumber)
{
  resetValidation();
  errFileUpload.Text = "";
  if (!Directory.Exists(Server.MapPath("~/Uploads/" + folderNumber.ToString())))
  {
    Directory.CreateDirectory(Server.MapPath("~/Uploads/" + folderNumber.ToString()));
  }
  if (uplFileUpload.UploadedFiles.Count > 0)
  {
    try
    {
      int cnt = 0;
      string msg = "";
      foreach (UploadedFile file in uplFileUpload.UploadedFiles)
      {
        msg = "";
        if (file.GetExtension() != "")
        {
          cnt++;
          file.SaveAs(Server.MapPath("~/Uploads/" + folderNumber.ToString() + "/" + file.GetName()), true);
        }
        else
        {
          msg = " All uploaded files must have an extension.";
        }
      }
      errFileUpload.Text = cnt.ToString() + " file(s) were uploaded." + msg;
    }
    catch (Exception)
    {
      errFileUpload.Text = "Unable to upload file(s)";
    }
  }
  else
  {
    errFileUpload.Text = "Select a file first";
  }
}


Matt
Tags
AsyncUpload
Asked by
WAMatt
Top achievements
Rank 2
Answers by
Plamen
Telerik team
WAMatt
Top achievements
Rank 2
Share this question
or