문제

I am trying to read an MS Project File using the .net version of MPJX in C# MVC3. I am using jQuery uploadify to upload the file to my controller.

Now, my problem is how to read the ProjectFile from my HttpPostedFileBase file = Request.Files[0]; I am getting an exception when reading the file because file.FileName doesn't contain the file's full path as part of browser security. If only I was good or at least have knowledge in java then I'd just convert the posted file to java.io.file as it's one of the valid parameters that ProjectReader.read supports.

Below is a code snippet of what I have right now (got the codes from the thread in Example of MPXJ library in C#).

 [HttpPost]
 public JsonResult UploadTask(int ProjectType)
    {
        try
        {
            HttpPostedFileBase file = Request.Files[0];
            MpxjReader.ProjectReader mppReader = MpxjReader.ProjectReaderUtility.getProjectReader(file.FileName);
            Mpxj.ProjectFile mpp = mppReader.read(file.FileName);

            List tables = mpp.getTables();
            Iterator iter = tables.iterator();
            while (iter.hasNext())
            {
                MpxjCore.Table table = (MpxjCore.Table)iter.next();
                if (table.getResourceFlag())
                {
                    List resources = mpp.getAllResources();
                    Iterator resourceIter = resources.iterator();
                    while (resourceIter.hasNext())
                    {
                        MpxjCore.Resource resource = (MpxjCore.Resource)iter.next();
                        List columns = table.getColumns();
                        Iterator columnIter = columns.iterator();
                        while (columnIter.hasNext())
                        {
                            MpxjCore.Column column = (MpxjCore.Column)columnIter.next();
                            Object columnValue = resource.getCachedValue(column.getFieldType());
                            Console.Write(columnValue);
                            Console.Write(",");
                        }
                        Console.WriteLine();
                    }
                }
                else
                {
                    List tasks = mpp.getAllTasks();
                    // etc. as above
                }
            }

            return Json(new { data = "success" }, JsonRequestBehavior.AllowGet);
        }
        catch
        {
            return Json(new { data = "error" }, JsonRequestBehavior.AllowGet);
        }
    }

Any help would be greatly appreciated.

도움이 되었습니까?

해결책

UPDATE: I'VE ALREADY SOLVED THE ISSUE BY SAVING THE .MPP FILE TO A SPECIFIED DIRECTORY FIRST THEN READ FROM THAT DIRECTORY (THIS IS HOW IT SHOULD BEHAVE WHEN DEPLOYED IN THE SERVER).

다른 팁

You can do it without storing the file to a filesystem, so you won't have to manage it. You can read the uploaded file directly using this code:

public ActionResult LoadProject(HttpPostedFileBase file)
{
    // Read file content into byte[]
    var buffer = new byte[file.InputStream.Length];
    file.InputStream.Read(buffer, 0, (int) file.InputStream.Length);                                    

    // Read the content using a new 'java' inputStream
    var reader = new MPPReader();                                  
    var project = reader.Read(new java.io.ByteArrayInputStream(buffer));

    // ...
}

I've implented as you, but we have a performance problem. To improve performance for your application, you can implement as below:

public ActionResult Upload(HttpPostedFileBase files)
{
   ProjectReader reader = new MPPReader();
   ProjectFile projectObj = reader.read(new ikvm.io.InputStreamWrapper(files.InputStream));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top