如何在项目文件夹中保存PDF文档? [英] How to save PDF document in project folder?

查看:108
本文介绍了如何在项目文件夹中保存PDF文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

Bellow是我的代码,它将数据表转换为PDF格式。这可以在浏览器站点下载,但我想将该pdf文件保存在Project的文件夹中。

还有一些额外的代码。我尝试了很多。但我不能。请帮助。



Hello,
Bellow is my code which convert the data table into PDF form. This can be download at browser site, But I want to save that pdf file in Project's folder.
there some additional code for this. I tried lots. But I cant. please Help.

public void ExportToPDF(string userAutoId)
    {
        //Get the data from database into datatable
        string uId = userAutoId.Substring(0, 8);
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("getContacts", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.AddWithValue("@userAutoId", userAutoId);
        //cmd.Parameters.Add("@retValue", System.Data.SqlDbType.VarChar).Direction = System.Data.ParameterDirection.ReturnValue; 
        da.Fill(dt);
    
        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=DataTable.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();

/*here I ant to code for save pdf file in project folder*/
        
        string strPath = HostingEnvironment.ApplicationPhysicalPath + @"TempFiles/";
        string fileName =uId + "ContactList.pdf";
        
        //PdfWriter writer = PdfWriter.GetInstance(pdfDoc new FileStream(strPath + fileName, FileMode.Create));
        //File.WriteAllText(strPath + fileName, sr.ToString());
        
        Response.Clear();
        Response.Write("{\"success\":true,\"isSuccess\":true,\"fileName\":\"" + fileName + "\"}");

        //Response.Write(pdfDoc);
        //Response.End();
    }   





我的尝试:



我必须在项目的本地文件夹中保存pdf文件..



What I have tried:

I have to save pdf file in project's local folder..

推荐答案

尝试 Server.MapPath()

Try Server.MapPath()
string fileName =uId + "ContactList.pdf";
string strPath = Server.MapPath(@"TempFiles/"+fileName);



参考: HttpServerUtility.MapPath Method(String)(System.Web) [ ^ ]

Hope ,它有帮助:)


Reference: HttpServerUtility.MapPath Method (String) (System.Web)[^]
Hope, it helps :)


试试这个

Try This
public string CommonFileSave(HttpPostedFileBase postedFile, string filePath)
       {
           string resultResponse = "sccuess";

           if (!Directory.Exists(filePath))
           {
               Directory.CreateDirectory(filePath);
               postedFile.SaveAs(filePath + postedFile.FileName);
           }
           else
           {
               filePath = filePath + postedFile.FileName;
               if (!System.IO.File.Exists(filePath))
               {
                   postedFile.SaveAs(filePath);
               }
           }
           return resultResponse;
       }


这篇关于如何在项目文件夹中保存PDF文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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