none
download image by client object Model : The remote server returned an error: (404) Not Found RRS feed

  • Question

  • Hello,

    •  I have an image in the following path in SharePoint

                http://servername/sites/Test/en-us/PublishingImages/activity.png

    • I write the following code to download image by client object Model :

               ClientContext clientContext = new ClientContext("http://ServerName/sites/Test");
               DownloadFile(clientContext, "/en-us/PublishingImages/", @"C:\Test", "activity.png");           

               public static bool DownloadFile(ClientContext context, string fileRelativeUrl, string destinationPath, string fileName)
                {
                    fileRelativeUrl = fileRelativeUrl.Replace("/Images/", "/PublishingImages/");
                    string sourceUrl = fileRelativeUrl + fileName;
                    string completeDestinationPath = destinationPath + fileName;

                    if (!System.IO.File.Exists(completeDestinationPath))
                    {
                        Directory.CreateDirectory(destinationPath);
                        FileInformation spFileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, sourceUrl);

                        using (Stream destination = System.IO.File.Create(completeDestinationPath))
                        {
                            for (int a = spFileInfo.Stream.ReadByte(); a != -1; a = spFileInfo.Stream.ReadByte())
                                destination.WriteByte((byte)a);
                        }
                    }
               }

    • When I run the code Th get the following error "The remote server returned an error: (404) Not Found."

          at this line "FileInformation spFileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, sourceUrl);"

        How Can I solve this error ?

        Thanks


    ASk

    Wednesday, June 18, 2014 6:57 AM

Answers

  • Hi,

    Greetings.

    Please check the source url value. Is it formed properly? try to print the value


    Please remember to click 'Mark as Answer' on the answer if it helps you

    • Marked as answer by himo Thursday, June 19, 2014 9:44 AM
    Wednesday, June 18, 2014 7:12 AM

All replies