Convert a document file to pdf in c# web api -


i saving data table. in table have column called attacheddocumentno pdf file. ok here table structure:

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)) [irequirementtypeid]     go      alter table [dbo].[tblrequirementtype] add  constraint [df_tblrequirementtype_szsubject]  default ('') [szsubject]     go      alter table [dbo].[tblrequirementtype] add  constraint [df_tblrequirementtype_iappid]  default ((-1)) [iapplicationdetailsid]     go     alter table [dbo].[tblrequirementtype] add  constraint [df_tblrequirementtype_iempid]  default ((-1)) [iempid]    go 

so created web api service post data table. following api class:

    [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();     } 

what want that, when saving database, should save szattacheddocumentno should saved pdf file in column.

i did search on internet , got piece of code , tried transformed need:

    public string uploadfiles(httpfilecollection strdocpath)     {         int iuploadedcnt = 0;          // define path want save 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 file count.         (int icnt = 0; icnt <= hfc.count - 1; icnt++)         {             httppostedfile hpf = hfc[icnt];              if (hpf.contentlength > 0)             {                 // check if selected file(s) exists in folder. (avoid duplicate)                 if (!file.exists(spath + path.getfilename(hpf.filename)))                 {                     // save files in folder.                     hpf.saveas(spath + path.getfilename(hpf.filename));                     iuploadedcnt = iuploadedcnt + 1;                 }             }         }          // return message (optional).         if (iuploadedcnt > 0)         {             return iuploadedcnt + " files uploaded successfully";         }         else         {             return "upload failed";         }     } 

cool. when run code , passed data swagger, have 2 different issues:

1-the first issue is, let's created docx file anywhere or in c:/ on machine , passes data follows:

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

i got error saying: "object reference not set instance of object." error, know passing wrong data somewhere, , realized it's @ 'szattacheddocumentno' after debugging code. tried following second issue:

2-here passed name of document "doc3.docx". data have been saved database that's not looking for:

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

}

i wnt way convert doc3.docx string in database, sorry in table...

thanks, appreciated.

somad y.

you need escape backslashes in path:

"c:\\files\\doc3.docx" 

your json missing comma @ end of iapplicationdetailsid line, , 01 isn't valid json number.

valid 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 } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -