【发布时间】:2019-01-23 04:32:46
【问题描述】:
我正在使用以下代码从 Angular 获取文件:
var httpPostedFile = HttpContext.Current.Request.Files["file"];
这是我的目录:
string folderExists = (HttpContext.Current.Server.MapPath("~/abc"));
如何将我的文件保存到该文件夹中?
for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
{
var FileName = httpPostedFile.FileName;
int fileSize = httpPostedFile.ContentLength;
byte[] fileByteArray = new byte[fileSize];
httpPostedFile.InputStream.Read(fileByteArray, 0, fileSize);
string fileLocation = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/uploads"), FileName);
if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/App_Data/uploads")))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/App_Data/uploads"));
httpPostedFile.SaveAs(fileLocation);
}
我正在将文件保存到文件夹中,但我想将这些文件存储在 SQL 数据库中。
我的文件结构:
public class FileTable
{
[Key]
public int Id { get; set; }
public byte[] FileData { get; set; }
public string FileName { get; set; }
}
如何将我的文件存储在数据库中?
【问题讨论】:
-
检查这可能会有所帮助:stackoverflow.com/questions/25125127/…
标签: c# asp.net-web-api