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.