Exemplo n.º 1
0
        public async Task <ActionResult <ResponseResult> > Upload([FromForm] FileForm form)
        {
            string scenario      = form.Scenario;
            string datastoreName = form.DatastoreName;
            var    file          = form.UploadFile;

            string excelDir = dsManager.GetUploadedFileSaveDir();

            Directory.CreateDirectory(excelDir);

            string savedFileName = null;

            if (file != null)
            {
                if (file.Length > 0)
                {
                    string newFileName = GenerateTempFileName(file.FileName);

                    var filePath = excelDir + Path.DirectorySeparatorChar + newFileName;

                    using (var stream = System.IO.File.Create(filePath))
                    {
                        await file.CopyToAsync(stream);
                    }

                    savedFileName = newFileName;
                }
            }

            ConvertFiles(savedFileName, scenario, datastoreName);

            ResponseResult msg = new ResponseResult();

            msg.success         = true;
            msg.responseMessage = "File: " + savedFileName + " has been received.\n";

            return(Ok(msg));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <ResponseResult> > Upload([FromForm] FileForm form)
        {
            string scenario      = form.Scenario;
            string datastoreName = form.DatastoreName;
            var    file          = form.UploadFile;


            string excelDir = dsManager.GetUploadedFileSaveDir();

            ResponseResult msg = new ResponseResult();

            try
            {
                Directory.CreateDirectory(excelDir);
            }
            catch (Exception e)
            {
                msg.success         = false;
                msg.responseMessage = "创建临时目录失败,请确认您有创建 " + excelDir + " 目录的权限";

                return(Ok(msg));
            }

            string savedFileName = null;

            if (file != null)
            {
                if (file.Length > 0)
                {
                    try
                    {
                        string newFileName = GenerateTempFileName(file.FileName);

                        var filePath = excelDir + Path.DirectorySeparatorChar + newFileName;

                        using (var stream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(stream);
                        }

                        savedFileName = newFileName;
                    }
                    catch (Exception e)
                    {
                        msg.success         = false;
                        msg.responseMessage = "在临时目录内保存 Excel 文件失败,请确认您有访问 " + excelDir + " 目录的权限";

                        return(Ok(msg));
                    }
                }
            }

            try
            {
                (bool success, string message, string targetDir) = ConvertFiles(savedFileName, datastoreName, scenario);

                if (success)
                {
                    if (this.dsManager.GetPersistanceType() == PersistanceType.MongoDB)
                    {
                        try
                        {
                            SmartKG.DataUploader.Executor.DataUploader uploader = new SmartKG.DataUploader.Executor.DataUploader();
                            uploader.UploadDataFile(targetDir, datastoreName);
                        }
                        catch (Exception e)
                        {
                            success = false;
                            message = "上传数据到 MongoDB 失败,请确认 MongoDB 链接";
                        }
                    }
                }

                msg.success = success;

                if (msg.success)
                {
                    msg.responseMessage = "File: " + savedFileName + " has been received.\n";
                }
                else
                {
                    msg.responseMessage = message;
                }
            }
            catch (Exception e)
            {
                msg.success         = false;
                msg.responseMessage = "解析 Excel 文件失败,请确认 Excel 有效。";
            }

            return(Ok(msg));
        }