1 - Если да, я думаю, что вы можете использовать хранимую процедуру для выполнения вашего запроса, например:
CREATE PROC spInsertArticle
@Title varchar(100),
@Content varchar(100),
@FileName varchar(500),
@FilePath varchar(500),
@FileLength varchar(500)
AS
Insert Into ArticleTable(Title,Content,FileName,
FilePath,FileLength,FileContentTime)
Values (@Title,@Content,@FileName,
@FilePath,@FileLength,GetDate())
2-Вернитесь к вашему проекту API и создайте класс модели с именем ArticleModel:
public class ArticleModel
{
public string Title {get; set; }
public string Content {get; set; }
public string FileName {get; set; }
public string FilePath {get; set; }
public string FileLength {get; set; }
}
3-Создайте метод api post в ArticleController
[Route("api/ArticleController/PostArticle")]
public HttpResponseMessage PostArticle(ArticleModel obj)
{
if (ModelState.IsValid)
{
try
{
string PhotoPath = Convert.ToString(ConfigurationManager.AppSettings["ImagePath"]);
ArticleModel newObj = new ArticleModel();
newObj.Title = obj.Title ;
newObj.Content = obj.Content;
newObj.FileName = obj.FileName;
newObj.FilePath = obj.FilePath;
newObjl.FileLength = obj.FileLength;
if (String.IsNullOrEmpty(newObj.Content))
{
}
else
{
string startingFilePath = PhotoPath;
string FilePath = SaveImage(newObj.Content, startingFilePath, newObj.FileName);
FileInfo fInfo = new FileInfo(FilePath);
newObj.Content = fInfo.Name;
}
ArticleEntities context = new ArticleEntities();
var newArticle = context.spInsertArticle(newObj.Title, newObj.Content,
newObj.FileName, newObj.FilePath, newObj.FileLength);
return Request.CreateResponse(HttpStatusCode.Created, newArticle);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
}
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
}
}
4-И, наконец, создайте метод SaveImage, упомянутый в методе PostArticle
private string SaveImage(string base64, string FilePath, string ImageName)
{
//Get the file type to save in
var FilePathWithExtension = "";
string localBase64 = "";
if (base64.Contains("data:image/jpeg;base64,"))
{
FilePathWithExtension = FilePath + ImageName + ".jpg";
localBase64 = base64.Replace("data:image/jpeg;base64,", "");
}
else if (base64.Contains("data:image/png;base64,"))
{
FilePathWithExtension = FilePath + ImageName + ".png";
localBase64 = base64.Replace("data:image/png;base64,", "");
}
else if (base64.Contains("data:image/bmp;base64"))
{
FilePathWithExtension = FilePath + ImageName + ".bmp";
localBase64 = base64.Replace("data:image/bmp;base64", "");
}
else if (base64.Contains("data:application/msword;base64,"))
{
FilePathWithExtension = FilePath + ImageName + ".doc";
localBase64 = base64.Replace("data:application/msword;base64,", "");
}
else if (base64.Contains("data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,"))
{
FilePathWithExtension = FilePath + ImageName + ".docx";
localBase64 = base64.Replace("data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,", "");
}
else if (base64.Contains("data:application/pdf;base64,"))
{
FilePathWithExtension = FilePath + ImageName + ".pdf";
localBase64 = base64.Replace("data:application/pdf;base64,", "");
}
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(localBase64)))
{
using (FileStream fs = new FileStream(FilePathWithExtension, FileMode.Create, FileAccess.Write))
{
//Create the specified directory if it does not exist
var photofolder = System.IO.Path.GetDirectoryName(FilePathWithExtension);
if (!Directory.Exists(photofolder))
{
Directory.CreateDirectory(photofolder);
}
ms.WriteTo(fs);
fs.Close();
ms.Close();
}
}
return FilePathWithExtension;
}
5-Попробуйте это в Почтальоне или чванстве, и это будет работать для вас.Я доступен для любого обсуждения