简体   繁体   English

如何将HttpPostedFileBase文件转换为Java.Io.InputStream?

[英]How to convert HttpPostedFileBase file to Java.Io.InputStream?

I'm working on ASP.net with the MPXJ library. 我正在使用MPXJ库在ASP.net上工作。 The .net version of MPXJ has been created using IKVM. .NET版本的MPXJ已使用IKVM创建。

Currently, I have a big problem: After upload a file (Microsoft Project file - .mpp file) to server (I don't need to save it), I want to convert from HttpPostedFileBase to the IKVM version of java.io.InputStream and MPXJ will manipulate them, but I don't know a way to implement this. 当前,我有一个大问题:将文件(Microsoft Project文件-.mpp文件)上传到服务器(不需要保存)之后,我想从HttpPostedFileBase转换为IKVM版本的java.io.InputStream和MPXJ会操纵它们,但我不知道实现该方法的方法。 My code: 我的代码:

public ActionResult Upload(HttpPostedFileBase files)
{
   // Todo: Convert from HttpPostedFileBase to Java.Io.InputStream
   ProjectReader reader = new MPPReader();
   ProjectFile projectObj = reader.read(Java.Io.InputStream);
}

You need a wrapper to provide a conversion between the IKVM Java type java.io.InputStream and a .net Stream instance. 您需要包装器才能在IKVM Java类型java.io.InputStream和.net Stream实例之间进行转换。 As luck would have it, IKVM ships with one... 幸运的是,IKVM附带了一个...

Using the wrapper, your example will now look like this: 使用包装器,您的示例现在将如下所示:

public ActionResult Upload(HttpPostedFileBase files)
{
   ProjectReader reader = new MPPReader();
   ProjectFile projectObj = reader.read(new ikvm.io.InputStreamWrapper(files.InputStream));
}

If you don't want to use IKVM, you can implement as below: 如果不想使用IKVM,可以执行以下操作:

public ActionResult Upload(HttpPostedFileBase files)
{
     byte[] fileData = null;
     using (var binaryReader = new BinaryReader(files.InputStream))
     {
        fileData = binaryReader.ReadBytes(files.ContentLength);
     }  
     ProjectFile projectObj = reader.read(new ByteArrayInputStream(fileData));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM