FileUpload.HasFile为多个文件上传返回“False” - ASP.net

时间:2016-12-08 06:28:56

标签: c# asp.net .net file-upload updatepanel

我正在使用ASP.net FileUpload控件在数据库中上传多个文件。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {


    @Override
    public void onStartup( ServletContext servletContext ) throws ServletException {
        super.onStartup( servletContext );

        RestfulServer server = new RestfulServer();
        servletContext.addServlet( "restful", server );
            /*
         * We want to support FHIR DSTU3 format. This means that the server
         * will use the DSTU3 bundle format and other DSTU3 encoding changes.
         *
         * If you want to use DSTU1 instead, change the following line, and change the 3 occurrences of dstu2 in web.xml to dstu1
         */
        server.setFhirContext( FhirContext.forDstu3() );
        WebApplicationContext myAppCtx = this.createRootApplicationContext( servletContext );

        // Get the spring context from the web container (it's declared in web.xml)

    /*
     * The BaseJavaConfigDstu3.java class is a spring configuration
     * file which is automatically generated as a part of hapi-fhir-jpaserver-base and
     * contains bean definitions for a resource provider for each resource type
     */
        List<IResourceProvider> beans = myAppCtx.getBean( "myResourceProvidersDstu3", List.class );
        server.setResourceProviders( beans );

        /*
         * The system provider implements non-resource-type methods, such as
         * transaction, and global history.
         */
        server.setPlainProviders( myAppCtx.getBean( "mySystemProviderDstu3", JpaSystemProviderDstu3.class ) );

    /*
     * The conformance provider exports the supported resources, search parameters, etc for
     * this server. The JPA version adds resource counts to the exported statement, so it
     * is a nice addition.
     */
        IFhirSystemDao<Bundle, Meta> systemDao = myAppCtx.getBean( "mySystemDaoDstu3", IFhirSystemDao.class );
        DaoConfig daoConfig = myAppCtx.getBean( DaoConfig.class );
        JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3( server, systemDao, daoConfig );
        confProvider.setImplementationDescription( "Example Server" );
        server.setServerConformanceProvider( confProvider );

        /*
         * Enable ETag Support (this is already the default)
         */
        server.setETagSupport( ETagSupportEnum.ENABLED );

    /*
     * This server tries to dynamically generate narratives
     */
        FhirContext ctx = server.getFhirContext();
        ctx.setNarrativeGenerator( new

                DefaultThymeleafNarrativeGenerator() );

        /*
         * Default to JSON and pretty printing
         */
        server.setDefaultPrettyPrint( true );
        server.setDefaultResponseEncoding( EncodingEnum.JSON );

        /*
         * -- New in HAPI FHIR 1.5 --
         * This configures the server to page search results to and from
         * the database, instead of only paging them to memory. This may mean
         * a performance hit when performing searches that return lots of results,
         * but makes the server much more scalable.
         */
        server.setPagingProvider( myAppCtx.getBean( DatabaseBackedPagingProvider.class ) );

    /*
     * Load interceptors for the server from Spring (these are defined in FhirServerConfig.java)
     */
        Collection<IServerInterceptor> interceptorBeans = myAppCtx.getBeansOfType( IServerInterceptor.class ).values();
        for (
                IServerInterceptor interceptor : interceptorBeans )

        {
            server.registerInterceptor( interceptor );
        }

        /*
         * If you are hosting this server at a specific DNS name, the server will try to
         * figure out the FHIR base URL based on what the web container tells it, but
         * this doesn't always work. If you are setting links in your search bundles that
         * just refer to "localhost", you might want to use a server address strategy:
         */
        //setServerAddressStrategy(new HardcodedServerAddressStrategy("http://example.com/fhir/baseDstu2"));

        /*
         * If you are using DSTU3+, you may want to add a terminology uploader, which allows
         * uploading of external terminologies such as Snomed CT. Note that this uploader
         * does not have any security attached (any anonymous user may use it by default)
         * so it is a potential security vulnerability. Consider using an AuthorizationInterceptor
         * with this feature.
         */
        server.registerProvider( myAppCtx.getBean( TerminologyUploaderProviderDstu3.class ) );


    }

    @Override
    protected SpringApplicationBuilder configure( final SpringApplicationBuilder builder ) {
        return builder;
    }

    public static void main( String[] args ) {
        configureApplication( new SpringApplicationBuilder() ).run( args );
    }

    private static SpringApplicationBuilder configureApplication( final SpringApplicationBuilder builder ) {
        return builder.sources( Application.class );
    }
}

在后面的代码中我使用<asp:UpdatePanel ID="UP_div_askQ" UpdateMode="Conditional" runat="server"> <ContentTemplate> <asp:FileUpload ID="FUQuestionFiles" CssClass="form-control" AllowMultiple="true" runat="server" /> <asp:Button ID="btnQSave" runat="server" CssClass="btn btn-success" Text="ASK QUESTION" OnClick="askQuestion" /> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="btnQSave" /> </Triggers> </asp:UpdatePanel> 检查文件是否存在。

HasFile

但是当选择了多个文件时,log.Debug("there are file" + FUQuestionFiles.HasFile.ToString()); 会返回false。

注意:如果只选择了一个文件,则返回true。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

FileUpload 有两个不同的属性,用于检查是否已上传任何文件:

<强> FileUpload.HasFile:

  

获取一个值,该值指示FileUpload控件是否包含   文件。

<强> FileUpload.HasFiles:

  

获取一个值,该值指示是否已上传任何文件

检查是否上传任何文件的最佳方法是同时检查HasFileHasFiles

if(fileUpload1.HasFile || fileUpload1.HasFiles)
    // do some code!

修改1:

您是否尝试在Page_Load

中添加以下内容
Page.Form.Attributes.Add("enctype", "multipart/form-data");

编辑2:

  

您能解释一下这行代码的不同之处吗?

来自W3C的Forms in HTML documents草案:

  

内容类型“application / x-www-form-urlencoded”效率低下   用于发送大量二进制数据或包含的文本   非ASCII字符。 内容类型“multipart / form-data” 应为   用于提交包含文件的表单,非ASCII数据和   二进制数据。

如果表单包含file input,则表单的enctype属性应设置为multipart/form-data

我认为您遇到了这个问题,因为您将FileUpload放在UpdatePanel内。