Answered by:
Rename Picture Library Image File Programmatically

Question
-
Dear Experts and Forum Users,
I am stuck with renaming an image file type before it is actually saved to picture library. I am achieving this using a custom web part with file upload and a simple button control.
Please look at the code below (I have added comments so that it is easy for you to understand excactly what I am doing)
If FileUpload.HasFile Then ' Get extension of existing file Dim ext as String = Path.GetExtension(FileUpload.PostedFile.FileName) ' Assign New Name - 'Test' FileUpload.SaveAs(how to declare this path + "Test" + ext) ' Upload the Image to Picture Library pictureLibraryList.RootFolder.Files.Add(FileUpload.FileName, streamImage) ' Finally Update the Picture Library pictureLibraryList.RootFolder.Update() End If
Problems with this code
Firstly, it is not working because fileupload control method saveAs() and fileupload control method postedFile.saveAs() both require a rooted path in this case C:\inetput\images and not http:\\url\ some url pointing to picture library - how do I actually rename the file and also save it to the picture library ?
Please can you suggest a workaround for this.
Many Thanks,
Monday, March 1, 2010 2:22 PM
Answers
-
Thanks all very much for all your input.
This is unbelieveable...!
Just provide the new file name in the following line
pictureLibraryList.RootFolder.Files.Add(abc, streamImage)
And it saves the uploaded image /file as ABC in the picture library. So no need for doing any fileUpload.SaveAs etc.
Thanks,- Marked as answer by .Net Frenzy Tuesday, March 2, 2010 10:46 AM
Tuesday, March 2, 2010 10:46 AM
All replies
-
Do you need to use your custom web part for anything besides renaming? If you don't really need the web part what I would do is let users upload via the standard sharepoint interface. I would then use a custom event handler on the library to handle the onAdded event (i.e. a new item was added). It is then quite easy to get a reference to the SPListItem which was added, get a reference to it's SPFile object and rename it.Monday, March 1, 2010 4:34 PM
-
Thanks very much for replying.
Actually, I am have to hide sharepoint interface (as complete site has been branded) from the users and that is why I have written a custom webpart which sits on the insert-new-image.aspx (blank webpart page created from browser). I know I could have done this using the itemAdded event receiver but would like to do it without it.
Although saying that - how would you do this normally? Because even picture library is a SPList isn't it? so how can we add a new item to this list?
eg -
Dim picLib as SPList = SPContext.Current.Web.Lists("myPictureLibrary")
Dim newItem as SPLIstItem = picLib.Items.Add
newItem("Name") = ??
newItem.Update()
Is this possible and how to actually pass new name to the file but upload the image from fileUpload control ?
Thanks.Monday, March 1, 2010 4:51 PM -
You can use the FileUpload's FileBytes or FileContent properties and the RootFolder.Files.Add("anynameUrl", FileBytes) or RootFolder.Files.Add("anynameUrl",FileContent).
certdev.com- Proposed as answer by Steve.Curran Tuesday, March 2, 2010 2:59 PM
Monday, March 1, 2010 8:49 PM -
You can use the FileUpload's FileBytes or FileContent properties and the RootFolder.Files.Add("anynameUrl", FileBytes) or RootFolder.Files.Add("anynameUrl",FileContent).
certdev.com
Dear Steve,
Many thanks for replying. Can you please provide some example on this or perhaps some online resource that you might know of. I am not able to figure it out using the example you mentioned.
Waiting to hear back from you..
Thanks,Tuesday, March 2, 2010 9:48 AM -
Thanks all very much for all your input.
This is unbelieveable...!
Just provide the new file name in the following line
pictureLibraryList.RootFolder.Files.Add(abc, streamImage)
And it saves the uploaded image /file as ABC in the picture library. So no need for doing any fileUpload.SaveAs etc.
Thanks,- Marked as answer by .Net Frenzy Tuesday, March 2, 2010 10:46 AM
Tuesday, March 2, 2010 10:46 AM -
Hi,
I also trying to upolad images to images library and change the file name before it gets added to library
u were referring to this line of code pictureLibraryList.RootFolder.Files.Add(abc, streamImage)
In that what does streamImage indicate?
Wednesday, October 6, 2010 4:50 AM -
Hi, Image Stream is a variable declared as stream because the image uploaded by user, I am getting it as a stream and then passing it to picture library. Please see the example here -
Dim streamImage As Stream = Nothing
If fleInput.HasFile Then
streamImage = fleInput.PostedFile.InputStream
End IfUsing Site As New SPSite("SITECOLLECTIONURL")
Using Web As SPWeb = Site.OpenWeb("SITEURL")
Dim pics As SPList = Web.Lists("PICTURELIBRARYNAME")
pics.RootFolder.Files.Add( _
fleInput.FileName, streamImage)
End Using
End UsingFinally you can also use this code here which is not with stream - http://www.eggheadcafe.com/community/aspnet/69/10138586/uploading-picture-in-picture-library-through-code.aspx
Hope this helps,
Thanks
Wednesday, October 6, 2010 9:17 AM