try
{
SPContext.Current.Web.AllowUnsafeUpdates = true;
SPList list = SPContext.Current.Web.Lists[“Testing1”];
SPListItem item = list.GetItemById(1);
SPAttachmentCollection attchmts = item.Attachments;
SPFile file = SPContext.Current.Web.GetFile(attchmts.UrlPrefix + attchmts[0]);
byte[] fileContent = file.OpenBinary();
SPList DestLibrary = SPContext.Current.Web.Lists[“Docs”];
//file.CopyTo(DestLibrary.RootFolder.Url + “/” + attchmts[0]);
SPFile uploadedFile = DestLibrary.RootFolder.Files.Add ( attchmts[0], fileContent, true);
DestLibrary.Update();
SPContext.Current.Web.AllowUnsafeUpdates = false;
}
catch (Exception exp)
{
Label1.Text = exp.StackTrace;
}
It works in SP2010 but in SP2013 throws exception
Server stack trace: at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Byte[] file, Boolean overwrite, String checkInComment, Boolean checkRequiredFields) at Microsoft.SharePoint.SPFileCollection_SubsetProxy.Add__Inner(String urlOfFile, Byte[] file, Boolean overwrite) at Microsoft.SharePoint.SPFileCollection_SubsetProxy.Add(String urlOfFile, Byte[] file, Boolean overwrite) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.SharePoint.SPFileCollection_SubsetProxy.Add(String urlOfFile, Byte[] file, Boolean overwrite) at Microsoft.SharePoint.SPFileCollection.Add__Inner(String urlOfFile, Byte[] file, Boolean overwrite) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Byte[] file, Boolean overwrite)
Changing SPFile uploadedFile = DestLibrary.RootFolder.Files.Add ( attchmts[0], fileContent, true); to
file.CopyTo(DestLibrary.RootFolder.Url + “/” + attchmts[0]);SPFile uploadedFile = DestLibrary.RootFolder.Files[DestLibrary.RootFolder.Url + “/” + attchmts[0]];
works.
DestLibrary.RootFolder.Files.Add – issue.