ALTER PROCEDURE INSERT_PDF
@DOCUMENT VARBINARY(MAX) = NULL
AS
BEGIN
INSERT INTO PDF
(Document)
VALUES
(@DOCUMENT)
END
GO
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using Newtonsoft.Json;
namespace pdf.Controllers
{
public class pdfController : ApiController
{
[HttpPost]
[ActionName("insertPdf")]
public int insertReceiptDocument(MemoryStream fileToPut)
{
//int varID = 0;
fileToPut = new MemoryStream();
byte[] document = fileToPut.ToArray();
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
//DateTime now = DateTime.Now;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "INSERT_PDF";
sqlCmd.Connection = myConnection;
sqlCmd.Parameters.Add("@DOCUMENT", SqlDbType.VarBinary, document.Length).Value = document;
myConnection.Open();
int rowInserted = sqlCmd.ExecuteNonQuery();
myConnection.Close();
return 1;
}
}
}
当使用 web api 在邮递员中传递文件时,0x 被插入到数据库中。想要将选定文件的二进制数据插入数据库。那么,如何在web api中将二进制数据形式的pdf文件插入到数据库中呢?