首页 > 解决方案 > 如何以角度(打字稿)将图像存储为字节数组

问题描述

我有两个应用程序,一个是 windows 应用程序,另一个是 web 应用程序。Windows 应用程序基于 C#,Web 应用程序基于 C# 和 Angular。他们都使用相同的数据库,我的问题是处理文件。不幸的是,文件内容已通过 Windows 应用程序存储在数据库中,所以我也必须在 Web 应用程序中处理这个问题。这是我在 win 应用程序中的代码:

string FileName = System.IO.Path.GetFullPath(_fnames[i]);// GetFileName(_fname);

string FileDescription = System.IO.Path.GetFullPath(_fnames[i]);    
string FileExtension = System.IO.Path.GetExtension(FileName);
System.IO.FileStream _fs = System.IO.File.Open(FileName, System.IO.FileMode.Open);
byte[] _f = new byte[_fs.Length];
_fs.Read(_f, 0, int.Parse(_fs.Length.ToString()));
byte[] FileContent = _f;
if (FileContent != null && FileContent.GetLength(0) > 0)
   {

     ReceiptDocDS.tblFilesRow _r = MainDS.tblFiles.NewtblFilesRow();
     _r.FileContent = FileContent;
     _r.FileDescription = FileDescription;
     // and other probs
  }

我想知道如何以角度处理这个问题。我想将文件的内容存储在fileContetprop. 所以我必须阅读文件的内容,我进行了研究,结果如下:

onUpload(event) {
    const reader = new FileReader();
    for(let file of event.files) {
        reader.onload = (e) => e.target.result;
        const test =reader.readAsDataURL(new Blob([file.objectURL.changingThisBreaksApplicationSecurity]));
        this.uploadedFiles.push(file);
    }
}

这就是this.file

lastModified: 1543877513859
lastModifiedDate: Tue Dec 04 2018 02:21:53 GMT+0330 (Iran Standard Time) {}
name: "ali.jpg"
objectURL: SafeUrlImpl
changingThisBreaksApplicationSecurity: "blob:http://localhost:4200/0ae15950-5bfd-448c-b002-04ed2f27e622"
__proto__: SafeValueImpl
size: 16196
type: "image/jpeg"
webkitRelativePath: ""

我希望在这一行中将图像的内容作为字节数组获取:

const test =reader.readAsDataURL(new Blob([file.objectURL.changingThisBreaksApplicationSecurity]));

但在测试中有注意事项。那么这怎么可能呢?非常感谢阅读的人。

标签: c#angulartypescriptcomponentsprimeng

解决方案


推荐阅读