Ng-File-Upload 将 PDF Blob 保存到数据库
Ng-File-Upload to Save PDF Blob to Database
我正在编辑此 post 以显示我根据以下建议进行的最新尝试。
我一直在搜索论坛,试图找到解决方案。我有一个 ASP.NET MVC 应用程序,我在其中使用 Angular。我正在尝试使用 danialfarid/ng-file-upload 允许用户上传 PDF,然后将其作为二进制数据保存到数据库(不是我的主意,但我必须这样做)。
我的 HTML 中有以下内容(取自示例):
File:<input type="file" ngf-select ng-model="picFile" name="file" accept="image/*" ngf-max-size="2MB" required ngf-model-invalid="errorFile"><br />
<img ngf-thumbnail="picFile" class="thumb"> <button ng-click="picFile = null" ng-show="picFile">Remove</button><br />
<button type="button" class="btn btn-primary" ng-click="uploadPic(picFile)">Upload</button>
在我的 Angular 控制器中:
$scope.uploadPic = function (files) {
file.upload = Upload.upload({
url: '/SSQV4/SSQV5/Document/UploadEMRDocument',
data: {file: files}
})
}
我的 MVC 控制器:
namespace SSQV5.Controllers
{
public class DocumentController : ApiController
{
public async Task<IHttpActionResult> UploadEMRDocument()
{
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var f = provider.Contents.First(); // assumes that the file is the only data
if (f != null)
{
var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
filename = Path.GetFileName(filename);
var buffer = await f.ReadAsByteArrayAsync();
//buffer now contains the file content,
//and filename has the original filename that was uploaded
//do some processing with it (e.g. save to database)
}
else
{
return BadRequest("Attachment failed to upload");
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok();
}
}
}
此代码根本不会访问 MVC 控制器。我显然遗漏了一些东西,但我对它可能是什么一无所知。非常感谢任何帮助!
配置上传时,指定 URL 文件将发布到的位置:
file.upload = Upload.upload({
url: 'myMVC/MyMethod',
data: {file: file}
})
您需要从表单数据中提取文件内容。
以下是我在我的应用程序中上传附件的方法(使用 ng-file-upload
的方式与您在前端使用的方式相同)。
public async Task<IHttpActionResult> UploadAttachment()
{
// Check if the request contains multipart/form-data.
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var f = provider.Contents.First(); // assumes that the file is the only data
if (f != null)
{
var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
filename = Path.GetFileName(filename);
var buffer = await f.ReadAsByteArrayAsync();
//buffer now contains the file content,
//and filename has the original filename that was uploaded
//do some processing with it (e.g. save to database)
}
else
{
return BadRequest("Attachment failed to upload");
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok();
}
我正在编辑此 post 以显示我根据以下建议进行的最新尝试。 我一直在搜索论坛,试图找到解决方案。我有一个 ASP.NET MVC 应用程序,我在其中使用 Angular。我正在尝试使用 danialfarid/ng-file-upload 允许用户上传 PDF,然后将其作为二进制数据保存到数据库(不是我的主意,但我必须这样做)。
我的 HTML 中有以下内容(取自示例):
File:<input type="file" ngf-select ng-model="picFile" name="file" accept="image/*" ngf-max-size="2MB" required ngf-model-invalid="errorFile"><br />
<img ngf-thumbnail="picFile" class="thumb"> <button ng-click="picFile = null" ng-show="picFile">Remove</button><br />
<button type="button" class="btn btn-primary" ng-click="uploadPic(picFile)">Upload</button>
在我的 Angular 控制器中:
$scope.uploadPic = function (files) {
file.upload = Upload.upload({
url: '/SSQV4/SSQV5/Document/UploadEMRDocument',
data: {file: files}
})
}
我的 MVC 控制器:
namespace SSQV5.Controllers
{
public class DocumentController : ApiController
{
public async Task<IHttpActionResult> UploadEMRDocument()
{
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var f = provider.Contents.First(); // assumes that the file is the only data
if (f != null)
{
var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
filename = Path.GetFileName(filename);
var buffer = await f.ReadAsByteArrayAsync();
//buffer now contains the file content,
//and filename has the original filename that was uploaded
//do some processing with it (e.g. save to database)
}
else
{
return BadRequest("Attachment failed to upload");
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok();
}
}
}
此代码根本不会访问 MVC 控制器。我显然遗漏了一些东西,但我对它可能是什么一无所知。非常感谢任何帮助!
配置上传时,指定 URL 文件将发布到的位置:
file.upload = Upload.upload({
url: 'myMVC/MyMethod',
data: {file: file}
})
您需要从表单数据中提取文件内容。
以下是我在我的应用程序中上传附件的方法(使用 ng-file-upload
的方式与您在前端使用的方式相同)。
public async Task<IHttpActionResult> UploadAttachment()
{
// Check if the request contains multipart/form-data.
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var f = provider.Contents.First(); // assumes that the file is the only data
if (f != null)
{
var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
filename = Path.GetFileName(filename);
var buffer = await f.ReadAsByteArrayAsync();
//buffer now contains the file content,
//and filename has the original filename that was uploaded
//do some processing with it (e.g. save to database)
}
else
{
return BadRequest("Attachment failed to upload");
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return Ok();
}