Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hello,
I'm using a Web Request to try and download an image from a remote server and store the results in the Media Library.
However when I run this code, I get the error:-
startIndex cannot be larger than length of string.
Here is the code, can anyone see where I'm going wrong:-
public void SaveMedia(newsCollateral NC, long newsId) { IMediaService ms = ApplicationContext.Current.Services.MediaService; var request = WebRequest.Create(NC.url); request.Timeout = 30000; WebResponse webResponse = request.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); var newStream = new MemoryStream(); responseStream.CopyTo(newStream); if (responseStream != null) { string fileName = NC.description.ToLower().Replace(',', '-').Replace(" ", ""); int folderId = 4368; //Folder Id in Media Library IEnumerable<IMedia> getAllChildItems = ms.GetChildren(folderId); bool folderExists = false; foreach (IMedia item in getAllChildItems.Where(c => c.ContentType.Name.Equals("Folder")).OrderByDescending(x => x.Name)) { if (item.Name == newsId.ToString()) { folderId = item.Id; folderExists = true; break; } } if (!folderExists) { try { var mediaFolder = ms.CreateMedia(newsId.ToString(), folderId, "Folder"); ms.Save(mediaFolder); } catch { throw new Exception("There was a problem creating a new folder - " + folderId); } } int assetID = 0; assetID = ms.GetChildren(folderId).Where(c => c.Name == fileName).Select(c => c.Id).FirstOrDefault(); if (assetID > 0) { try { IMedia existingFile = ms.GetById(assetID); existingFile.SetValue("umbracoFile", fileName, newStream); ms.Save(existingFile); } catch { throw new Exception("There was a problem updating Image - " + assetID); } } else { try { if (newStream.Length > 0) { IMedia mediaFile = ms.CreateMedia(fileName, folderId, "Image"); mediaFile.SetValue("umbracoFile", fileName, newStream); ms.Save(mediaFile); } } catch (Exception ex) { throw new Exception("There was a problem saving the image - " + fileName + " : (" + folderId + ") " + ex.Message); } } } responseStream.Close(); }
It has finally dawned on me what I was doing wrong.
The filename being passed through in the SetValue was not the original filename it was the modified one.
For those interested, here is the final code:-
public void SaveMedia(newsCollateral NC, long newsId) { IMediaService ms = ApplicationContext.Current.Services.MediaService; string fileName = NC.description.ToLower().Replace(',', '-').Replace(" ", ""); bool folderExists = false; int folderId = 4368; //Folder Id in Media Library var request = WebRequest.Create(NC.url); request.Timeout = 30000; using (var response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) { if (response.StatusCode == System.Net.HttpStatusCode.OK) { Stream streamCopy = new MemoryStream(); stream.CopyTo(streamCopy); IEnumerable<IMedia> getAllChildItems = ms.GetChildren(folderId); #region "Loop through all folders in to check if folder exists" foreach (IMedia item in getAllChildItems.Where(c => c.ContentType.Name.Equals("Folder")).OrderByDescending(x => x.Name)) { if (item.Name == newsId.ToString()) { folderId = item.Id; folderExists = true; break; } } #endregion #region "Create Folder If It Doesn't Exist" if (!folderExists) { try { var mediaFolder = ms.CreateMedia(newsId.ToString(), folderId, "Folder"); ms.Save(mediaFolder); } catch { throw new Exception("There was a problem creating a new folder - " + folderId); } } #endregion #region "Either Add the File or Update the Existing one" int assetID = 0; assetID = ms.GetChildren(folderId).Where(c => c.Name == fileName).Select(c => c.Id).FirstOrDefault(); string origFilename = ""; Uri uri = new Uri(NC.url); origFilename = System.IO.Path.GetFileName(uri.LocalPath); if (assetID > 0) { try { IMedia existingFile = ms.GetById(assetID); existingFile.SetValue("umbracoFile", origFilename, streamCopy); ms.Save(existingFile); } catch { throw new Exception("There was a problem updating Image - " + assetID); } } else { try { IMedia mediaFile = ms.CreateMedia(fileName, folderId, "Image"); mediaFile.SetValue("umbracoFile", origFilename, streamCopy); ms.Save(mediaFile); } catch (Exception ex) { throw new Exception("There was a problem saving the image - " + fileName); } } #endregion } } }
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.
Continue discussion
Downloading Remote Image via WebRequest and Saving to Umbraco Media Library
Hello,
I'm using a Web Request to try and download an image from a remote server and store the results in the Media Library.
However when I run this code, I get the error:-
startIndex cannot be larger than length of string.
Here is the code, can anyone see where I'm going wrong:-
It has finally dawned on me what I was doing wrong.
The filename being passed through in the SetValue was not the original filename it was the modified one.
For those interested, here is the final code:-
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.