2016-11-10 50 views
1

我将一些数据保存到表中。在表格中,我有一个名为AttachedDocumentNo的列,它将成为pdf文件。好的这里是我的表格结构:在c#web api中将文档文件转换为pdf

CREATE TABLE tblRequirementType(
    [pkId] [bigint] IDENTITY(1,1) NOT NULL, 
    [szDescription] [varchar](max) NULL, 
    [iRequirementTypeId] [bigint] NOT NULL, 
    [szRequirementNumber] [varchar](100) NULL, 
    [szRequirementIssuer] [varchar](200) NULL, 
    [szOrganization] [varchar](250) NULL, 
    [dIssuedate] [datetime] NULL, 
    [dExpirydate] [datetime] NULL, 
    [szSignedBy] [varchar](250) NULL, 
    [szAttachedDocumentNo] [varchar](200) NULL, 
    [dStampdate] [datetime] NULL, 
    [szSubject] [varchar](250) NULL, 
    [iApplicationDetailsId] [bigint] NULL, 
    [iEmpId] [bigint] NULL, 
    CONSTRAINT [PK_tblRequirementType] PRIMARY KEY CLUSTERED 
    (
     [pkId] ASC 
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

    GO 

    SET ANSI_PADDING OFF 
    GO 

    ALTER TABLE [dbo].[tblRequirementType] ADD CONSTRAINT [DF_tblRequirementType_iRequirementTypeId] DEFAULT ((-1)) FOR [iRequirementTypeId] 
    GO 

    ALTER TABLE [dbo].[tblRequirementType] ADD CONSTRAINT [DF_tblRequirementType_szSubject] DEFAULT ('') FOR [szSubject] 
    GO 

    ALTER TABLE [dbo].[tblRequirementType] ADD CONSTRAINT [DF_tblRequirementType_iAppId] DEFAULT ((-1)) FOR [iApplicationDetailsId] 
    GO 

    ALTER TABLE [dbo].[tblRequirementType] ADD CONSTRAINT [DF_tblRequirementType_iEmpId] DEFAULT ((-1)) FOR [iEmpId] 
    GO 

所以我创建了一个web api服务来将数据发布到表中。 以下是我的API类:

[Route("PostRequirementTypeProcessing")] 
    public IEnumerable<NPAAddRequirementTypeProcessing> PostRequirementTypeProcessing(mdlAddAddRequirementTypeProcessing requTypeProcess) 
    { 
     mdlAddAddRequirementTypeProcessing rTyeProcessing = new mdlAddAddRequirementTypeProcessing(); 

     HttpFileCollection hfc = HttpContext.Current.Request.Files; 

     //Filename = requTypeProcess.szSubject; 
     rTyeProcessing.szDescription = requTypeProcess.szDescription; 
     rTyeProcessing.iRequirementTypeId = requTypeProcess.iRequirementTypeId; 
     rTyeProcessing.szRequirementNumber = requTypeProcess.szRequirementNumber; 
     rTyeProcessing.szRequirementIssuer = requTypeProcess.szRequirementIssuer; 
     rTyeProcessing.szOrganization = requTypeProcess.szOrganization; 
     rTyeProcessing.dIssuedate = requTypeProcess.dIssuedate; 
     rTyeProcessing.dExpirydate = requTypeProcess.dExpirydate; 
     rTyeProcessing.szSignedBy = requTypeProcess.szSignedBy; 
     rTyeProcessing.szAttachedDocumentNo = requTypeProcess.szAttachedDocumentNo; 
     if (String.IsNullOrEmpty(rTyeProcessing.szAttachedDocumentNo)) 
     { 

     } 
     else 
     { 
      UploadFiles(hfc); 
     } 
     rTyeProcessing.szSubject = requTypeProcess.szSubject; 
     rTyeProcessing.iApplicationDetailsId = requTypeProcess.iApplicationDetailsId; 
     rTyeProcessing.iEmpId = requTypeProcess.iEmpId; 

     NPAEntities context = new NPAEntities(); 
     Log.Debug("PostRequirementTypeProcessing Request traced"); 

     var newRTP = context.NPAAddRequirementTypeProcessing(requTypeProcess.szDescription, requTypeProcess.iRequirementTypeId, 
            requTypeProcess.szRequirementNumber, requTypeProcess.szRequirementIssuer, requTypeProcess.szOrganization, 
            requTypeProcess.dIssuedate, requTypeProcess.dExpirydate, requTypeProcess.szSignedBy, 
            requTypeProcess.szAttachedDocumentNo, requTypeProcess.szSubject, requTypeProcess.iApplicationDetailsId, 
            requTypeProcess.iEmpId); 

     return newRTP.ToList(); 
    } 

什么我想要做的是,保存到数据库时,它应该保存一切,但szAttachedDocumentNo应保存在其列PDF文件。

我没有在互联网上搜索,并得到了一段代码,并试图将其转化为我的需要:

public string UploadFiles(HttpFileCollection strDocPath) 
    { 
     int iUploadedCnt = 0; 

     // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES. 
     string sPath = ""; 
     //sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/locker/"); 

     sPath = Convert.ToString(ConfigurationManager.AppSettings["ProfilePath"]); 

     //HttpFileCollection hfc = HttpContext.Current.Request.Files; 
     HttpFileCollection hfc = strDocPath; 

     // CHECK THE FILE COUNT. 
     for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++) 
     { 
      HttpPostedFile hpf = hfc[iCnt]; 

      if (hpf.ContentLength > 0) 
      { 
       // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE) 
       if (!File.Exists(sPath + Path.GetFileName(hpf.FileName))) 
       { 
        // SAVE THE FILES IN THE FOLDER. 
        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName)); 
        iUploadedCnt = iUploadedCnt + 1; 
       } 
      } 
     } 

     // RETURN A MESSAGE (OPTIONAL). 
     if (iUploadedCnt > 0) 
     { 
      return iUploadedCnt + " Files Uploaded Successfully"; 
     } 
     else 
     { 
      return "Upload Failed"; 
     } 
    } 

酷。当我运行代码并将数据传递给了我,我有两个不同的问题:我有两个不同的问题:

1-第一个问题是,假设我在我的机器上的任意位置或c:/创建了一个docx文件,如下:

{ 
    "szDescription": "Business Registration", 
    "iRequirementTypeId": 30012, 
    "szRequirementNumber": "BR3363347G", 
    "szRequirementIssuer": "Environment Protection Agency", 
    "szOrganization": "Blue Ocean Limited", 
    "dIssuedate": "2014-02-09 00:00:00.000", 
    "dExpirydate": "2018-02-09 00:00:00.000", 
    "szSignedBy": "Somad", 
    "szAttachedDocumentNo": "C:\Files\Doc3.docx", 
    "szSubject": "Sub1", 
    "iApplicationDetailsId": 01 
    "iEmpId": 40021 
} 

我得到一个错误:“对象引用未设置为对象的实例。” 对于这个错误,我知道我在某处传递了错误的数据,并且在调试完代码后,我意识到它位于'szAttachedDocumentNo'处。所以我尝试了以下我的第二个问题:

2 - 在这里,我只是传递文件的名称是“Doc3.docx”。所有的数据都被保存到数据库中,这不是我所期待的:

{ 
    "szDescription": "Business Registration", 
    "iRequirementTypeId": 30012, 
    "szRequirementNumber": "BR3363347G", 
    "szRequirementIssuer": "Environment Protection Agency", 
    "szOrganization": "Blue Ocean Limited", 
    "dIssuedate": "2014-02-09 00:00:00.000", 
    "dExpirydate": "2018-02-09 00:00:00.000", 
    "szSignedBy": "Somad", 
    "szAttachedDocumentNo": "Doc3.docx", 
    "szSubject": "Sub1", 
    "iApplicationDetailsId": 01 
    "iEmpId": 40021 

}

我只是Wnt信号的方式向doc3.docx转换为数据库中的字符串,在表遗憾。 ..

谢谢,任何帮助,将不胜感激。

Somad Y.

回答

0

你一定要逃逸路径中的反斜杠:

"C:\\Files\\Doc3.docx" 

您的JSON也缺少在iApplicationDetailsId线的末端,逗号和01是不是有效Json号码。

有效的JSON:

{ 
    "szDescription": "Business Registration", 
    "iRequirementTypeId": 30012, 
    "szRequirementNumber": "BR3363347G", 
    "szRequirementIssuer": "Environment Protection Agency", 
    "szOrganization": "Blue Ocean Limited", 
    "dIssuedate": "2014-02-09 00:00:00.000", 
    "dExpirydate": "2018-02-09 00:00:00.000", 
    "szSignedBy": "Somad", 
    "szAttachedDocumentNo": "C:\\Files\\Doc3.docx", 
    "szSubject": "Sub1", 
    "iApplicationDetailsId": 1, 
    "iEmpId": 40021 
} 
+0

感谢stuartd。我现在正在获取表格中的文件路径。现在我怎样才能将它转换为pdf并将其保存到共享文件? – Somad

+0

[有很多将Word文件转换为PDF的选项](https://www.google.co.uk/search?q=convert+word+document+to+pdf+c%23) – stuartd