2016-11-10 12 views
1

一部のデータをテーブルに保存しています。テーブルには、私はPDFファイルになるAttachedDocumentNoと呼ばれる列があります。私のテーブル構造はここにあります: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 - 最初の問題は、私がどこでもdocxファイルを作成したとしましょう。私のマシン上のc:/にデータを渡して、次のようにデータを渡します。次のようになります。

{ 
    "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つの問題を試しました:

2 - ここで私は "Doc3.docx"という文書の名前を渡しました。私はちょうどテーブルで申し訳ありません、データベース内の文字列にdoc3.docxを変換する方法をWNT

{ 
    "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 

}

:すべてのデータは、私が探しています何ではありませんデータベースに保存されています。 ..

ありがとう、どんな助けもありがとう。あなたがパスにバックスラッシュをエスケープする必要が

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

関連する問題