将文件保存到目录,然后 [英] Save file to directory and then

查看:116
本文介绍了将文件保存到目录,然后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
由于我是这个网站的新手,所以我希望有人能够在这个方面为我提供帮助.
这是我的两难境地.

以下代码允许我访问多个.pdf文件,然后将它们合并为1个pdf文件.此时,我想将文件保存到虚拟目录.合并部分效果很好,但我能完成的只是传输文件以供下载.
任何想法都欢迎.


代码在第一页合并cs
下面

Hello All
Since I am new to this site I am hoping someone will be able to assist me in this are.
Here my dilemma.

The following code allows me to access multiple.pdf files and then merge them into 1 pdf file. At this point I would like to save the file to a virtual directory. The merge part works great but all I can get accomplished is to transmit the file for download.
Any and all idea are welcome.


Code is below page one Merge cs

public static void MergeFiles(string destinationFile, string[] sourceFiles)
        {
            try
            {
                int f = 0;
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(sourceFiles[f]);
                // we retrieve the total number of pages
                int n = reader.NumberOfPages;
                Console.WriteLine("There are " + n + " pages in the original file.");
                // step 1: creation of a document-object
                Document document = new Document(reader.GetPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
                // step 3: we open the document
                document.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page;
                int rotation;
                // step 4: we add content
                while (f < sourceFiles.Length)
                {
                    int i = 0;
                    while (i < n)
                    {
                        i++;
                        document.SetPageSize(reader.GetPageSizeWithRotation(i));
                        document.NewPage();
                        page = writer.GetImportedPage(reader, i);
                        rotation = reader.GetPageRotation(i);
                        if (rotation == 90 || rotation == 270)
                        {
                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                        }
                        Console.WriteLine("Processed page " + i);
                    }
                    f++;
                    if (f < sourceFiles.Length)
                    {
                        reader = new PdfReader(sourceFiles[f]);
                        // we retrieve the total number of pages
                        n = reader.NumberOfPages;
                        Console.WriteLine("There are " + n + " pages in the original file.");
                    }
                }
                // step 5: we close the document
                document.Close();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine(e.StackTrace);
            }
        }
    }


结束文件


End file

推荐答案

为什么要将文件保存到服务器上的目录中?您尝试了什么,遇到了什么错误?
Why would you want to save the file to a directory on the server ? What have you tried, and what errors do you get ?


没有一些背景很难解释,但是就可以了.
我是该网站的所有者,而不是原始程序员.他走了,我必须捡起碎片

该网站的成员可以从iphone和Ipad通过ftp访问特定的文件夹.我要完成的工作是获取通过merge.cs创建的合并文件,并将其保存到该文件夹​​中,以便他们可以通过FTP访问它.然后,一旦创建了文件,并且如果成员选择加入,我也想通过电子邮件将其作为附件发送并通过SMTP发送.但是现在一次只能做一件事.我对使用C#asp.net或任何其他语言进行编程的经验不是很丰富.所以就叫我一个新手.任何帮助,将不胜感激.这是页面的其余部分.
我已经从该站点上当前正在运行的其他网页上放了一些零碎的内容,但我一直停留在此tempfile问题上.

It difficult to explain without some background but here goes.
I am the owner of this website but not the original programmer. He gone and I have to pickup the pieces

This website has members that access a specific folder via ftp from the iphone and Ipad. What I am trying to accomplish is to take the merged file that is being created via the merge.cs and save it to that folder so that they may access it via FTP. Then once the file is created and if the member opts in I also want to email it as an attachment and send it via SMTP. But right now One thing at a time. I am not very experienced in programming in C# asp.net or any other language. So just call me a newbie. Any help would be appreciated. This is the rest of the page.
I have put bits and piece from other webpage that are currently working on the site but I am stuck on this tempfile issue.

private void SaveFile(string tempFile)
       {
           long userId = -1;
           userId = Convert.ToInt64(Session["userId"]);
           foreach (UploadedFile file in tempFile)
               if (file.ContentLength < 20000000)
               {
                   if (userId > 0)
                   {
                       UserDAO objUserDAO = new UserDAO();
                       User objUser = objUserDAO.GetUser(userId);
                       if (objUser != null)
                           strTargetFolder = "~/" + strDataDir + "/" + objUser.UserName;
                       string targetFolder = Server.MapPath(strTargetFolder);
                       string targetFileName = Path.Combine(targetFolder, file.GetName());
                       targetFileName = getFileName(strTargetFolder, targetFileName, targetFileName, 0);
                       if (!Directory.Exists(targetFolder))
                       {
                           Directory.CreateDirectory(targetFolder);
                       }
                       file.SaveAs(targetFileName);
                   }
               }


       }


       protected void RadUpload1_FileExists(object sender, Telerik.Web.UI.Upload.UploadedFileEventArgs e)
       {
           int counter = 1;
           UploadedFile file = e.UploadedFile;
           string targetFolder = Server.MapPath(RadUpload1.TargetFolder);
           string targetFileName = Path.Combine(targetFolder, file.GetNameWithoutExtension()) + counter.ToString() + file.GetExtension();

           while (File.Exists(targetFileName))
           {
               counter++;
               targetFileName = Path.Combine(targetFolder, file.GetNameWithoutExtension()) + counter.ToString() + file.GetExtension();
           }

           file.SaveAs(targetFileName);
           /*if (userId > 0)
           {
               FileCabinetDAO objFileCabinetDAO = new FileCabinetDAO();
               FLMapping.FLLibrary.Model.FileCabinet objFileCabinet = objFileCabinetDAO.getFileCabinet
               if (objBGC != null)
               {
                   BGCdocsDAO objBGCdocsDAO = new BGCdocsDAO();
                   BGCdocs objBGCdocs = new BGCdocs();

                   objBGCdocs.BGCdetails = objBGC;
                   objBGCdocs.FilePath = file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension();//file.GetName();
                   objBGCdocs.IsCSA = false;
                   objBGCdocsDAO.save(objBGCdocs);
               }
           }*/
       }

       protected string getFileName(string targetFolder, string originalFilePath, string filePath, int counter)
       {
           if (File.Exists(filePath))
           {
               counter++;
               filePath = Server.MapPath(Path.Combine(targetFolder, Path.GetFileNameWithoutExtension(originalFilePath)) + counter.ToString() + Path.GetExtension(filePath));
               filePath = getFileName(targetFolder, originalFilePath, filePath, counter);
           }
           return filePath;
       }



       // Need to keep working here on attachment and then also saving it in member ftp Directory BA 070510
       private void SendEmail(string strFileName)
       {
           long userId = long.Parse(Session["UserID"].ToString());
           UserDAO objUserDAO = new UserDAO();
           User objUser = objUserDAO.GetUser(Convert.ToInt64(userId));
           string strSendEmail = null;
           if(objUser.ProflightEmail != null)
               strSendEmail = objUser.ProflightEmail.Trim();
           else
               strSendEmail = objUser.Email.Trim();

           string UsName = objUser.LastName;

           MailMessage mailMessage = new MailMessage ();
           mailMessage.From = new MailAddress("NoReply@Proflightlog.com");
           mailMessage.To.Add(strSendEmail);


           mailMessage.Subject = "ProFlightLog Charting Service -TPP Procedures Attached";
           mailMessage.Body = FormatMessage1(UsName);

            if (strFileName != null)
            {

                // Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));
                    Attachment attach = new Attachment(Server.MapPath(strFileName));
                    /* Attach the newly created email attachment */
                    mailMessage.Attachments.Add(attach);
                    /* Store the attach filename so we can delete it later */
                   string attach1 = strFileName;

            }
       }


这篇关于将文件保存到目录,然后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆