2

我想在c#中将图像保存在mongodb中,下面是代码。

[HttpPost] 私有 AssetData GetAssetFromHttpRequest(HttpRequestMessage requestMessage, out MemoryStream imageStream)

    {
        imageStream = null;
        var streamProvider = new MultipartFormDataStreamProvider(Path.GetTempPath());
        var bodyPartFileNames = streamProvider.FileData;
        var fileinfo = bodyPartFileNames.Select(item => new FileInfo(item.LocalFileName)).FirstOrDefault();
        var fileName = Path.GetFileName(bodyPartFileNames[0].Headers.ContentDisposition.FileName.Replace("\"", string.Empty));
        // validate the file info.
        if (fileinfo != null)
        {
            using (var fileStream = File.OpenRead(fileinfo.FullName))
            {
                imageStream = new MemoryStream();
                imageStream.SetLength(fileStream.Length);
                fileStream.Read(imageStream.GetBuffer(), 0, (int)fileStream.Length);
            }

            // create an asset instance with the details of attached, for the time being the type of the image is set as image. 
            return new AssetData { AssetName = fileName, Id = 1254 };
        }
        return null;
    }
4

0 回答 0