Как сохранить файл, хранящийся в столбце VARBINARY, в папку? - PullRequest
0 голосов
/ 31 октября 2018

Я сохранил свой файл в SQL Server в виде столбца VARBINARY в таблице. Здесь я пытаюсь извлечь этот контент и сохранить его в виде файла в папке:

List<FileData> newData = new List<FileData>();

var c = from n in db.FileTable
        where n.Id == 1
        select n;

foreach(var _somedata in c)
{
    var abc = new FileData
                {
                    _FileData = _somedata.FileData,
                    FileName = _somedata.FileName,
                };

    newData.Add(abc);
    string Img_name = abc.FileName;
    string folder_path = System.Web.HttpContext.Current.Server.MapPath("~/Images");

    if (!Directory.Exists(folder_path))
    {
        Directory.CreateDirectory(folder_path);
    }

    string imgPath = Path.Combine(folder_path, Img_name);
    ...
}

Как мне сохранить мой файл в этой папке?

1 Ответ

0 голосов
/ 01 ноября 2018

Поскольку это двоичный файл, вы можете использовать метод System.IO.File.WriteAllBytes():

foreach(var _somedata in c)
{
    var abc = new FileData
                {
                    _FileData = _somedata.FileData,
                    FileName = _somedata.FileName,
                };

    string Img_name = abc.FileName;
    string folder_path = System.Web.HttpContext.Current.Server.MapPath("~/Images");

    if (!Directory.Exists(folder_path))
    {
        Directory.CreateDirectory(folder_path);
    }

    string imgPath = Path.Combine(folder_path, Img_name);

    // this call will write out all the bytes (content) of your file
    // to the file name you've specified   
    File.WriteAllBytes(imgPath, abc._FileData);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...