string fileNameonly = Fileupload1.FileName; // Only file name.
private byte[] ToByteArray(Stream inputStream)
{
using (MemoryStream ms = new MemoryStream())
{
inputStream.CopyTo(ms);
return ms.ToArray();
}
}
private void AddFileToDocumentLibrary(string documentLibraryUrl, string filename, string Title)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(documentLibraryUrl))
{
using (SPWeb web = site.OpenWeb())
{
Stream StreamImage = null;
if (Fileupload1.HasFile)
{
StreamImage = Fileupload1.PostedFile.InputStream;
}
byte[] file_bytes = ToByteArray(StreamImage);
web.AllowUnsafeUpdates = true;
SPDocumentLibrary documentLibrary = (SPDocumentLibrary)web.Lists["DocumentLibraryName"];
SPFileCollection files = documentLibrary.RootFolder.Files;
SPFile newFile = files.Add(documentLibrary.RootFolder.Url + "/" + filename, file_bytes, true);
SPList documentLibraryAsList = web.Lists["DocumentLibraryName"];
SPListItem itemJustAdded = documentLibraryAsList.GetItemById(newFile.ListItemAllFields.ID);
SPContentType documentContentType = documentLibraryAsList.ContentTypes["Document"]; //amend with your document-derived custom Content Type
itemJustAdded["ContentTypeId"] = documentContentType.Id;
itemJustAdded["Title"] = Title;
itemJustAdded.Update();
web.AllowUnsafeUpdates = false;
}
}
});
}
private byte[] ToByteArray(Stream inputStream)
{
using (MemoryStream ms = new MemoryStream())
{
inputStream.CopyTo(ms);
return ms.ToArray();
}
}
private void AddFileToDocumentLibrary(string documentLibraryUrl, string filename, string Title)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(documentLibraryUrl))
{
using (SPWeb web = site.OpenWeb())
{
Stream StreamImage = null;
if (Fileupload1.HasFile)
{
StreamImage = Fileupload1.PostedFile.InputStream;
}
byte[] file_bytes = ToByteArray(StreamImage);
web.AllowUnsafeUpdates = true;
SPDocumentLibrary documentLibrary = (SPDocumentLibrary)web.Lists["DocumentLibraryName"];
SPFileCollection files = documentLibrary.RootFolder.Files;
SPFile newFile = files.Add(documentLibrary.RootFolder.Url + "/" + filename, file_bytes, true);
SPList documentLibraryAsList = web.Lists["DocumentLibraryName"];
SPListItem itemJustAdded = documentLibraryAsList.GetItemById(newFile.ListItemAllFields.ID);
SPContentType documentContentType = documentLibraryAsList.ContentTypes["Document"]; //amend with your document-derived custom Content Type
itemJustAdded["ContentTypeId"] = documentContentType.Id;
itemJustAdded["Title"] = Title;
itemJustAdded.Update();
web.AllowUnsafeUpdates = false;
}
}
});
}
No comments:
Post a Comment