简体   繁体   中英

“The remote server returned an error: (401) Unauthorized” error when trying to upload a file

I found a lot of question about (401) Unauthorized error, but none of them explained to me how to diagnose the issue.

I created a new MVC ASP.net app to learn how to upload a file to a sharepoint folder. As of right now, I can't seem to be able to copy a file from C:\\testfolder\\file.txt to C:\\testfolder\\UploadedFiles

Here is the method that I made where I send in the source file path and the target folder:

 //Flag to indicate whether file was uploaded successfuly or not
        bool isUploaded = true;
        try
        {
            // Create a PUT Web request to upload the file.
            WebRequest request = WebRequest.Create(targetDocumentLibraryPath);

            //Set credentials of the current security context
            request.PreAuthenticate = true;
            request.UseDefaultCredentials = true;
            ICredentials credentials = new NetworkCredential( "Username", "password", "Domain"); //I used my username and password here
            request.Credentials = credentials;
            //request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "PUT";

            // Create buffer to transfer file
            byte[] fileBuffer = new byte[1024];

            // Write the contents of the local file to the request stream.
            using (Stream stream = request.GetRequestStream())
            {
                //Load the content from local file to stream
                using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read))
                {
                    //Get the start point
                    int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
                    for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
                    {
                        stream.Write(fileBuffer, 0, i);
                    }

                }
            }

            // Perform the PUT request
            WebResponse response = request.GetResponse();

            //Close response
            response.Close();
        }
        catch (Exception ex)
        {
            //Set the flag to indiacte failure in uploading
            isUploaded = false; //The remote server returned an error: (401) Unauthorized.
        }

        //Return the final upload status
        return isUploaded; 
    }

I tried the following:

  • Changing the permission of the folders to be able to write/read from them for all users.
  • Use other folder locations (SharePoint, local drive, network drive) to upload the file to.

Any idea on how to get this problem to work? Any prospective on how to debug the problem is also appreciated.

Update: It is probably being my account is set as the app pool account in IIS. I am still not sure what the issue is.

This wouldn't be something like the proxy settings would it?

Do you need to add the default proxy setting to your

WebRequest 

Also do you need to add

<system.net> 
  <defaultProxy useDefaultCredentials="true" /> 
</system.net> 

To your web config?

This may help.

How should I set the default proxy to use default credentials?

我决定尝试另一种方法,并使用此处描述的httpposted文件和.saveAs()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM