2

In my project, i need to download the image and do some checking, then delete and re-upload it. However, i got an error when i trying to download the image.

    foreach (SPListItem item in docLib.Items)//SPList
    {
        SPFile file = item.File;
        if (file != null)
        {
            string fileURL = file.Url;  //libraryname/foldername/filename.extension
            string fileName = file.Name;//filename.extension
            string folderURL = fileURL.Substring(0, fileURL.Length - (fileName.Length + 1));//libraryname/foldername
            string fileExtension = fileName.Substring(fileName.Length - 5);// .extension, assume it may contain 4 chars. (e.g. .tiff)

            Console.WriteLine("file url: " + fileURL);
            Console.WriteLine("folder url: " + folderURL);


            if (too.checkExtension(fileExtension))
            {
                Console.WriteLine("True");// is image type

                    WebClient wc = new WebClient();
                    byte[] bytes = wc.DownloadData(siteurl + fileURL);
                    MemoryStream ms = new MemoryStream(bytes);
                    Image myImage = Image.FromStream(ms);


                    otherClass.Checking(myImage); // my job
                    item.Delete();// delete item directly 

                    web.Files.Add(string.Format("{0}/{1}", fileURL, fileName), bytes, false);// upload it to original place


            }
        }
    }

The error is about:

unhandled Exception: System.Net.WebException: The remote server returned an error: <401> Unauthorized. at System.Net.WebClient.DownloadDataInternal

So, how to fix it? Moreover, can anyone tell me the feasibility of my delete and upload part.

2

You need to use the CSOM (Client Object Model) To Download your image

Here you use WebClient with this, you loose your sharePoint context, and so your are not connected to sharePoint when want to download image by using : byte[] bytes = wc.DownloadData(siteurl + fileURL);
Try to use methode of the SPFile object, like OpenBinaryStream to get the Byte of your file in a Stream.

| improve this answer | |
  • This application is running at SharePoint Server. Is it sill need to use CSOM? – king yau Aug 26 '16 at 9:45
  • Sorry ! I did not see that you are in server. You can (and must) use Sharepoint Object Model and it's more easy to use ;). You can find a lot of blog/tuto which explains how to upload/edit/delete files. Here an exemple how to download: Download files from SharePoint and How to: Upload a File – Nico Aug 26 '16 at 13:20
0

No need for CSOM.

WebClient, by default, does not pass on credentials. You will need to configure it to either use specific credentials or the credentials of the current user using wc.UseDefaultCredentials (see: https://stackoverflow.com/questions/12212116/how-to-get-httpclient-to-pass-credentials-along-with-the-request for an explanation).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.