ASP.Net WebApi2提交带有pd​​f / doc文件的表单

时间:2019-02-26 15:26:09

标签: c# asp.net-web-api2 form-submit .net-framework-version

我正在尝试将一些数据(名称,姓氏,文件名,文件内容)上载到数据库,但是当我按下“提交”按钮时,它会进入带有值PostUser的函数null和我收到了这个error

这是我的代码的一部分:

模型(cs)

public class User
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Nome { get; set; }

    [Required]
    public string Cognome { get; set; }

    public string FileName { get; set; }

    public byte[] PdfContent { get; set; }
}

ViewModel(cs)

public class PdfViewModel
{
    /// <summary>
    /// L'oggetto pdf che verrà salvato a database
    /// </summary>
    public User User { get; set; }

    /// <summary>
    /// Il file che viene postato dalla finestra di upload: non ha ancora il datatype richiesto dal modello Pdf
    /// Perciò creo una nuova proprietà per elaborarlo in binario prima di inserirlo nella table Pdfs
    /// </summary>
    public HttpPostedFile HttpPostedFileBase { get; set; }

}

控制器(后用户)

[ResponseType(typeof(PdfViewModel))]
public async Task<IHttpActionResult> PostUser(PdfViewModel pdfViewModel)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    pdfViewModel.User.PdfContent = ProvaUnoManager.GetEncodedFile(pdfViewModel.HttpPostedFileBase);
    pdfViewModel.User.FileName = pdfViewModel.HttpPostedFileBase.FileName;
    db.Users.Add(pdfViewModel.User);
    db.SaveChanges();

    await db.SaveChangesAsync();

    return CreatedAtRoute("DefaultApi", new { id = pdfViewModel.User.Id }, pdfViewModel);
}

这是一个ASP.NET Web应用程序(.NET Framework)

0 个答案:

没有答案