AjaxControlToolKit AjaxFileUpload…如何允许所有文件类型和没有扩展名的文件?

huangapple go评论53阅读模式
英文:

AjaxControlToolKit AjaxFileUpload...how to allow all file types and files with no extension?

问题

I can help you with the translation. Here is the translated content:

无法弄清如何使AjaxControlToolKit的AjaxFileUpload允许上传所有文件类型。我尝试了下面的代码,但它不起作用。任何帮助将不胜感激。

<ajaxToolkit:AjaxFileUpload ID="AjaxFileUploader" runat="server" Width="100%" Style="padding-bottom: 10px" OnClientUploadStart="UploadStart" OnClientUploadCompleteAll="UploadEnd" AllowedFileTypes="*" AutoStartUpload="True"/>

这是我的web.config文件的内容......

这是我的配置文件和控件标记的外观(经过Albert D. Kallal建议的更改)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="devExpress">
      <section name="themes" type="DevExpress.Web.ThemesConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="compression" type="DevExpress.Web.CompressionConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="settings" type="DevExpress.Web.SettingsConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="errors" type="DevExpress.Web.ErrorsConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="resources" type="DevExpress.Web.ResourcesConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="bootstrap" type="DevExpress.Web.Bootstrap.BootstrapConfigurationSection, DevExpress.Web.Bootstrap.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
    </sectionGroup>
    <section name="ajaxControlToolkit" type="AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit" requirePermission="false" />
  </configSections>
  <ajaxControlToolkit additionalUploadFileExtensions="dcm," />
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="AjaxFileUploadHandler" verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler,AjaxControlToolkit" />
      <add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="DX.ashx" name="ASPxHttpHandlerModule" preCondition="integratedMode" />
      <add type="DevExpress.Web.ASPxUploadProgressHttpHandler, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="ASPxUploadProgressHandlerPage.ashx" name="ASPxUploadProgressHandler" preCondition="integratedMode" />
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" />
    </modules>
    <staticContent>
      <mimeMap fileExtension=".dcm" mimeType="application/dicom" />
    </staticContent>
  </system.webServer>
  <appSettings>
  bla bla bla bla
...

<ajaxToolkit:AjaxFileUpload ID="AjaxFileUploader" runat="server" Width="100%" Style="padding-bottom: 10px" OnClientUploadStart="UploadStart" OnClientUploadCompleteAll="UploadEnd" AllowedFileTypes="dcm,zip" AutoStartUpload="True"/>

Please note that the code parts remain unchanged in this translation.

英文:

I cant figure out how to get AjaxControlToolKit's AjaxFileUpload to allow uploading of all file types. I tried the below code but it doest work. Any help would be greatly appreciated.

<ajaxToolkit:AjaxFileUpload ID="AjaxFileUploader" runat="server" Width="100%" Style="padding-bottom: 10px" OnClientUploadStart="UploadStart" OnClientUploadCompleteAll="UploadEnd" AllowedFileTypes="*" AutoStartUpload="True"/>

this is what my web.config file looks like....

Here is what my config file and control markup looks like (after changes suggested by Albert D. Kallal)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="devExpress">
      <section name="themes" type="DevExpress.Web.ThemesConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="compression" type="DevExpress.Web.CompressionConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="settings" type="DevExpress.Web.SettingsConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="errors" type="DevExpress.Web.ErrorsConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="resources" type="DevExpress.Web.ResourcesConfigurationSection, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
      <section name="bootstrap" type="DevExpress.Web.Bootstrap.BootstrapConfigurationSection, DevExpress.Web.Bootstrap.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
    </sectionGroup>
    <section name="ajaxControlToolkit" type="AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit" requirePermission="false" />
  </configSections>
  <ajaxControlToolkit additionalUploadFileExtensions="dcm," />
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="AjaxFileUploadHandler" verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler,AjaxControlToolkit" />
      <add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="DX.ashx" name="ASPxHttpHandlerModule" preCondition="integratedMode" />
      <add type="DevExpress.Web.ASPxUploadProgressHttpHandler, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="ASPxUploadProgressHandlerPage.ashx" name="ASPxUploadProgressHandler" preCondition="integratedMode" />
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v22.2, Version=22.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" />
    </modules>
    <staticContent>
      <mimeMap fileExtension=".dcm" mimeType="application/dicom" />
    </staticContent>
  </system.webServer>
  <appSettings>
  bla bla bla bla

...

<ajaxToolkit:AjaxFileUpload ID="AjaxFileUploader" runat="server" Width="100%" Style="padding-bottom: 10px" OnClientUploadStart="UploadStart" OnClientUploadCompleteAll="UploadEnd" AllowedFileTypes="dcm,zip" AutoStartUpload="True"/>

答案1

得分: 1

好的,以下是翻译后的部分:

这是一个有点未经记录的问题。

虽然您“可以”指定允许的文件类型?

有一些“安全”限制,如.msg文件和其他一些文件。

所以,在Web配置中,您可以更改这个。

在web.config中,在“configuration”后面添加这个,就像这样:

<configuration>

  <configSections>

    <section name="ajaxControlToolkit" 
             type="AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit" 
             requirePermission="false"/>
  </configSections>

  <ajaxControlToolkit additionalUploadFileExtensions="msg" />

  <appSettings>
  .etc .etc. etc.

因此,您必须添加“section name”,然后(只有这样),才能添加additionalUploadFileExtensions部分。

默认情况下,允许的文件扩展名列表如下:

7z, aac, avi, bz2, csv, doc, docx, gif, gz, htm, html, jpeg, jpg, md, mp3, mp4

ods, odt, ogg, pdf, png, ppt, pptx, svg, tar, tgz, txt, xls, xlsx, xml, zip

请注意,"bmp"文件不在该列表中!!!

因此,虽然您可以像这样过滤/限制zip和pdf:

<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" 
                            AllowedFileTypes="pdf,zip" 
    ChunkSize="8192" 
    OnClientUploadStart="UpLoadStart" 
    OnClientUploadComplete="UpLoadCompleteOne" 
    OnClientUploadCompleteAll="UpLoadDone" ClientIDMode="Static"
    ViewStateMode="Enabled" 
    OnClientUploadError="UpLoadError" title="" MaximumNumberOfFiles="20"
    onchange="fileschanged();" />

所以,在上面,我们只允许用户使用pdf和zip。

然而,如果我们添加了:

AllowedFileTypes="pdf,zip,bmp,msg"

它将不允许。

因此,有一个内置的“允许”文件类型列表。

您可以更改这个。在Web配置中,添加这个部分:

<configuration>

  <configSections>

    <section name="ajaxControlToolkit" 
             type="AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit" 
             requirePermission="false"/>
  </configSections>

  <ajaxControlToolkit additionalUploadFileExtensions="msg" />

  <appSettings>
    <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\" />

请注意,第二个“additional”文件元素首先需要上面的aj toolkit的配置部分。因此,查找现有的配置部分,如果没有,然后添加上面的部分。

对于没有文件扩展名的文件?

似乎这个可以工作:

<ajaxControlToolkit additionalUploadFileExtensions="msg," />

请注意上面的末尾的“,”。

作为信息?

我相信白名单或限制的文件出现在aj toolkit的第17版左右,目前的版本号大约是20左右。因此,如果您使用的版本大于17,那么您将不得不添加上述内容到web.config以“允许”这些文件类型,而不管您在页面标记中为ajaxfileupload控件指定的实际文件类型是什么。

编辑:允许没有扩展名的文件

经过测试,发现Web配置不必更改以允许没有扩展名的文件。上述文件的白名单(允许的文件)仍然有效,如果想要上传一个简单的“bmp”文件,那么只需更改web.config 是必需的。

但是,要允许没有扩展名的文件,不需要对web.config进行任何更改,只需在页面上的控件标记中的允许文件类型列表中使用这个。

例如:

AllowedFileTypes=",zip"

所以,请注意上面的“空”文件类型(相当确定它必须是第一个条目)。

上面的内容将允许上传没有扩展名的文件。

编辑和跟进

结果发现,不需要更改web.config以允许上传没有文件扩展名的文件。

所需的仅仅是在控件标记中添加一个“空”的文件类型,例如:

AllowedFileTypes=",zip"

所以,“空”的文件类型(没有空格)似乎是有效的,而不需要更改web.config文件。

但是,请记住内置的“白名单”允许的文件类型。这个允许的列表对于具有扩展名的文件仍然有效。

所以,对于任何不在文件的白名单中的文件类型(扩展名)?那么是的,您必须修改web.config,然后这样的文件才能被上传。可以自由地在页面上的标记中添加“允许”的文件类型(扩展名)或在标记中留空允许文件类型属性以允许所有类型的文件。

在任何情况下(在标记中使用允许的文件类型或不使用属性),都必须修改允许的文件类型“列表”以允许不在默认白名单文件类型中的文件。

英文:

Ok, this is a bit of a un-documented issue.

While you "can" specify the file types that are allowed?

There are some "security" restrictions such as .msg files and a few others.

So, in web config, you can change this.

Add this right after the start in web.config, in "configuration"

like this:

&lt;configuration&gt;


  &lt;configSections&gt;

    &lt;section name=&quot;ajaxControlToolkit&quot; 
             type=&quot;AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit&quot; 
             requirePermission=&quot;false&quot;/&gt;
  &lt;/configSections&gt;

  &lt;ajaxControlToolkit additionalUploadFileExtensions=&quot;msg&quot; /&gt;


  &lt;appSettings&gt;
  .etc .etc. etc.

So, you MUST add the "section name", and then (and ONLY then), can you add the
additionalUploadFileExtensions part.

The ALLOWED list of file extenstions by default is this:

 7z,aac,avi,bz2,csv,doc,docx,gif,gz,htm,html,jpeg,jpg,md,mp3,mp4

 ods,odt,ogg,pdf,png,ppt,pptx,svg,tar,tgz,txt,xls,xlsx,xml,zip

NOTE very close, that "bmp" files are not in that list!!!

So, while you can filter/restrict/limit say zip and pdf like this:

    &lt;ajaxToolkit:AjaxFileUpload ID=&quot;AjaxFileUpload1&quot; runat=&quot;server&quot; 
                                AllowedFileTypes=&quot;pdf,zip&quot; 
        ChunkSize=&quot;8192&quot; 
        OnClientUploadStart=&quot;UpLoadStart&quot; 
        OnClientUploadComplete = &quot;UpLoadCompleteOne&quot; 
        OnClientUploadCompleteAll=&quot;UpLoadDone&quot; ClientIDMode=&quot;Static&quot;
        ViewStateMode=&quot;Enabled&quot; 
        OnClientUploadError=&quot;UpLoadError&quot; title=&quot;&quot; MaximumNumberOfFiles=&quot;20&quot;
        onchange=&quot;fileschanged();&quot; /&gt;

so, in above, we ONLY allow the user pdf, zip.

However, if we added say :

AllowedFileTypes=&quot;pdf,zip,bmp,msg&quot;

it will NOT allow this.

So, there is a biult in "list" of allowable file types.

You can change this. In web config, add this section:

&lt;configuration&gt;


  &lt;configSections&gt;

    &lt;section name=&quot;ajaxControlToolkit&quot; 
             type=&quot;AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit&quot; 
             requirePermission=&quot;false&quot;/&gt;
  &lt;/configSections&gt;

  &lt;ajaxControlToolkit additionalUploadFileExtensions=&quot;msg&quot; /&gt;


  &lt;appSettings&gt;
&lt;add key=&quot;ChartImageHandler&quot; value=&quot;storage=file;timeout=20;dir=c:\TempImageFiles\;&quot; /&gt;

Note carefull, the 2nd "additional" file element FIRST requires the config section above for the aj toolkit. So, look for a existing one, and if not, then add the above.

And for NO file extenstion?

This seems to work:

  &lt;ajaxControlToolkit additionalUploadFileExtensions=&quot;msg,&quot; /&gt;

Note the trailing "," in above.

As a FYI?

I believe the file "whitelist" or restrictions appeared around version 17 for the aj toolkit, and the current number is around 20 or so. (so, if you using later then version 17, then you will have to add the above to web.config to "allow" those file types regardless of the actual file types you specify for the ajaxfileupload control in the page markup.

Edit: allowing files without extensions

It turns out from testing, that web config does NOT have to be changed to allow files without extensions. The above white list of files (allowed) still remains, and if one wants to up-load say a simple "bmp" file, then one MUST change the web.config.

However, to allow files without a extension, no such changes to web.config is required, only that you use this in the list of allowed file types in the control's markup on the page.

eg:

AllowedFileTypes=&quot;,zip&quot;

So, note the "empty" file type in above. (quite sure it has to be the first entry).

The above will thus allow up-loading of files without an extension.

Edit and follow up

It turns out that the web.config changes are NOT required to allow a file being up-loaded without a file extension.

All that is required is the in the control markup is to add a "empty" file type, say like this:

AllowedFileTypes=&quot;,zip&quot;

So, a "empty" file type (no space) seems to work, and their is NOT a requirement to change the web config file.

However, do keep in mind that the advice about the built-in "white list" of allowed file types. This allowed list remains in effect for files with extensions.

So, for ANY type of file (extension) that is NOT in the white-list of files? Then yes, you do have to modify web.config, and then such files can be up-loaded. One can freely add "allowed" file types (extensions) to the markup tag on the page, or leave out the allowed file types attribute to allow all types of files.

In either case (using allowed file types or not attribute in markup), the allowed file type "list" has to be modified to allow files not in that default white-list of file types.

So, allowed file types tag/attribute is optional, but the allowed file types not in the above "white-list" of files requires a change to web.config for such files to be up-loaded.

huangapple
  • 本文由 发表于 2023年2月14日 21:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定