How do I upload large (> 25MB) files to a web service?

后端 未结 8 1827
野趣味
野趣味 2020-12-07 15:56

I have a web service that takes a byte[] and saves it.

This works fine for \"small\" files, but once I hit a certain size the web service fails and returns \"The req

相关标签:
8条回答
  • 2020-12-07 16:19

    If I was stuck having to use web services and needed to support very large files I would look at implementing a system that allows you to upload files in pieces.

    Eg.

    • ticketId GetTicket(size)
    • UploadData(ticketId, byte[] payload) (this can be called as many times as you want)
    • FinalizeUpload(ticketId)

    This would allow you to chunk up the big uploads, and not hold too much data in memory. The disadvantage is that you are still using a fairly inefficient transport mechanism.

    0 讨论(0)
  • 2020-12-07 16:23

    You should keep in mind that web services aren't primarily designed as file transfer mechanisms. Any purpose-designed file transfer protocol will likely do a better job than a web service. For instance, such protocols are more likely to deal with error recovery, partial uploads, etc.

    However, if you're going to use web services for this purpose in .NET, you should use WCF, if at all possible. Among other benefits, WCF handles streaming, and will therefore be a lot more efficient in terms of memory usage. I'm concerned that if you follow the two (accurate) suggestions above, your next result will be "out of memory or resources" exceptions, as the old ASMX technology tries to load your entire 25MB file into memory at once. In fact, it may have several copies in memory at the same time!

    0 讨论(0)
  • 2020-12-07 16:24

    Just to add information to people googling this web.config:

    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI

    <location path="Copy.asmx"> <!-- Name of you asmx -->
        <system.webServer>
          <security>
            <requestFiltering>
              <requestLimits maxAllowedContentLength="104857600"/> <!-- 100 megs -->
            </requestFiltering>
          </security>
        </system.webServer>
      </location>
    

    This solved our problem after troubleshooting this issue for quite som time.

    0 讨论(0)
  • 2020-12-07 16:26

    maxRequestLength is in KB, not bytes. This should give you a 30 MB limit within a 4-minute timeout window.

    <httpRuntime executionTimeout="240" maxRequestLength="30000" />
    

    Having numbers that are too high may be actually preventing your values from being applied. I think I ran into this a few years ago when I thought it was a byte limit (vague memory).

    0 讨论(0)
  • 2020-12-07 16:36

    In addition to the httpRuntime/maxRequestLength mentioned in the question, it looks like there is an additional item that can be added to the web service's web.config file to permit large file transfers.

      <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="2000000000" />
          </requestFiltering>
        </security>
      </system.webServer>
    

    This appears to enable larger files to be uploaded via web services.

    0 讨论(0)
  • 2020-12-07 16:36

    this worked for me:

                <binding name="uploadFilesBasicHttpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" receiveTimeout="00:10:10" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00">
                    <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
                    <security mode="TransportWithMessageCredential">
                        <message clientCredentialType="UserName"/>
                    </security>
                </binding>
    
    0 讨论(0)
提交回复
热议问题