How To Enable Upload In IIS

2019-08-09 20:33发布

问题:

I have a folder on my IIS instance called Uploads. I am doing an HTTP POST from my Android application to upload a file to the IIS server.

This isn't working. The operation looks like this (this is part of the failed request logging),

<failedRequest url="http://*ADDRESS*:80/JsonWCFService/Uploads"
           siteId="1"
           appPoolId=".NET4 App Pool"
           processId="1176"
           verb="POST"
           remoteUserName=""
           userName=""
           tokenUserName="NT AUTHORITY\IUSR"
           authenticationType="anonymous"
           activityId="{00000000-0000-0000-6800-0080000000FA}"
           failureReason="STATUS_CODE"
           statusCode="200"
           triggerStatusCode="405"
           timeTaken="63"
           xmlns:freb="http://schemas.microsoft.com/win/2006/06/iis/freb"
           >

So we are getting a 405 there. Do I need to configure something more in IIS?

I am using the PhoneGap API to upload the file using the FileTransfer.upload method.

回答1:

IIS is a web server and it does not come with a build-in upload handler. If you would like to upload files using POST on a specific url you would have to create an actual web-application.

Read more here: http://support.microsoft.com/kb/189651

The simplest solution that I can propose is to create a new asp.net project with a HttpHandler - the code could look something like this (as simple as it can be):

public class UploadHandler : IHttpHandler, IRequiresSessionState
{        
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            HttpPostedFile file = context.Request.Files[0];
            file.SaveAs(path);
        }
        catch (Exception ex)
        {
            // include your custom logging code
            // Log.Error(ex.Message, ex);
            throw;
        }
    }
}

Please keep in mind that this is acceptable for small files, you should not use it for something heavier than couple of MB.



标签: http iis cordova