如何将Web表单转换为pdf并上传到sharepoint文档库?

时间:2014-04-25 09:23:17

标签: c# asp.net sharepoint

我有一个网络部分,用户在其上写下他们的信息。我需要做的是在用户点击“保存”按钮后将这些信息保存为sharepoint文档库。

我正在使用itextsharp,到目前为止我所做的是在SP服务器c:\ drive中创建PDF并将其上传到sarepoint文档库。但我想做的是没有在c:\ drive中创建文档。谢谢

//creates document in c:// drive
Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream(Page.Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
            document.Open();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Parse(new StringReader(content));
            document.Close();

//then uploads to sharepoint librart
            String fileToUpload = Page.Request.PhysicalApplicationPath +"\\MySamplePDF.pdf";
            String sharePointSite = "http://testportal/";
            String documentLibraryName = "mydefdoclibi";

            using (SPSite oSite = new SPSite(sharePointSite))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    if (!System.IO.File.Exists(fileToUpload))
                        throw new FileNotFoundException("File not found.", fileToUpload);

                    SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                    // Prepare to upload
                    Boolean replaceExistingFiles = true;
                    String fileName = System.IO.Path.GetFileName(fileToUpload);
                    FileStream fileStream = File.OpenRead(fileToUpload);

                    // Upload document
                    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

                    // Commit 
                    myLibrary.Update();
                }
            }

1 个答案:

答案 0 :(得分:2)

因此使用MemoryStream而不是写入文件

using(MemoryStream ms = new MemoryStream())
{
    PdfWriter.GetInstance(document, ms, FileMode.Create));

    ...
    ms.Position = 0;
    SPFile spfile = myLibrary.Files.Add(fileName, ms, replaceExistingFiles);
...
}