예제 #1
0
        private static void Callback(string fileUploadDataJSON)
        {
            Debug.Log("Callback called " + fileUploadDataJSON);

            if (FileUploadEventHandler == null)
            {
                Debug.Log("FileUploadEventHandler == null");
                return;
            }

            UploadedFileInfo[] files;
            if (!string.IsNullOrEmpty(fileUploadDataJSON))
            {
                files = JsonUtility.FromJson <FileUploadResult>(fileUploadDataJSON).files;
                FileUploadEventHandler.Invoke(files);
            }
            else
            {
                files = new UploadedFileInfo[0] {
                };
                FileUploadEventHandler.Invoke(files);
            }

            //Debug.Log (FileUploadEventHandler);
        }
예제 #2
0
        public bool Upload(Stream stream, FapFileInfo fileInfo, FileUploadEventHandler updateEvent)
        {
            string fileRepositoryPath = GetFilePath();

            if (fileInfo == null || stream == null)
            {
                return(false);
            }
            //文件存储策略
            FileDirectoryStrategy strategy = new FileDirectoryStrategy();

            string lv = _configService.GetSysParamValue("file.directory.level");

            if (lv.IsPresent())
            {
                strategy.Level = lv.ToInt();
            }
            else
            {
                strategy.Level = 3;
            }
            string fileName       = "";
            string directoryPath  = strategy.GetFullPath("" + fileInfo.FileId, out fileName);
            string fullpathToSave = Path.Combine(fileRepositoryPath, directoryPath);

            try
            {
                if (!Directory.Exists(fullpathToSave))
                {
                    Directory.CreateDirectory(fullpathToSave);
                }
                string fileToSave = Path.Combine(fullpathToSave, fileName + fileInfo.FileSuffix);

                using (var outStream = new FileStream(fileToSave, FileMode.Create, FileAccess.Write))
                {
                    using (stream)
                    {
                        stream.CopyTo(outStream);
                        if (updateEvent != null)
                        {
                            updateEvent(fileInfo, new FileUploadEventArgs()
                            {
                                Total         = stream.Length,
                                Uploaded      = stream.Length,
                                FileFullName  = Path.Combine(directoryPath, fileName + fileInfo.FileSuffix),
                                FileName      = fileName + fileInfo.FileSuffix,
                                FileDirectory = directoryPath
                            });
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }
            return(false);
        }
        /// <summary>
        /// This method is called when a file is uploaded. It triggers the Upload event.
        /// </summary>
        /// <param name="e">The information about the file that was uploaded.</param>
        protected virtual void OnUpload(FileUploadEventArgs e)
        {
            FileUploadEventHandler handler = (FileUploadEventHandler)base.Events[EventUploadKey];

            if (handler != null)
            {
                handler(this, e);
            }
        }
예제 #4
0
        public bool Upload(Stream stream, FapFileInfo fileInfo, FileUploadEventHandler updateEvent)
        {
            FapAttachment attachment = _dataAccessor.Get <FapAttachment>((int)fileInfo.FileId);

            if (attachment != null)
            {
                using (var inStream = stream)
                {
                    byte[] bytes = new byte[inStream.Length];
                    inStream.Read(bytes, 0, bytes.Length);
                    attachment.FileContent = bytes;
                }

                _dataAccessor.Update <FapAttachment>(attachment);
            }

            return(true);
        }
예제 #5
0
        public bool Upload(Stream stream, FapFileInfo fileInfo, FileUploadEventHandler updateEvent)
        {
            if (fileInfo == null || stream == null)
            {
                return(false);
            }
            FtpStrategy strategy = new FtpStrategy();

            string lv = _configService.GetSysParamValue("ftp.directory.level");

            if (lv.IsPresent())
            {
                strategy.Level = lv.ToInt();
            }
            else
            {
                strategy.Level = 3;
            }
            string fileName         = "";
            string directoryPath    = strategy.GetFullPath("" + fileInfo.FileId, out fileName);
            string destPath_to_save = Path.Combine(ftpRootPath, directoryPath);
            string filePath_to_save = Path.Combine(destPath_to_save, fileName + fileInfo.FileSuffix);

            try
            {
                using (FtpClient conn = new FtpClient())
                {
                    conn.Host        = host;
                    conn.Credentials = new NetworkCredential(username, password);
                    conn.Connect();
                    if (!conn.DirectoryExists(destPath_to_save))
                    {
                        conn.CreateDirectory(destPath_to_save);
                    }

                    using (Stream outStream = conn.OpenWrite(filePath_to_save))
                    {
                        using (var inStream = stream)
                        {
                            stream.CopyTo(outStream);

                            if (updateEvent != null)
                            {
                                updateEvent(fileInfo, new FileUploadEventArgs()
                                {
                                    Total         = inStream.Length,
                                    Uploaded      = inStream.Length,
                                    FileFullName  = Path.Combine(directoryPath, fileName + fileInfo.FileSuffix),
                                    FileName      = fileName + fileInfo.FileSuffix,
                                    FileDirectory = directoryPath
                                });
                            }
                        }
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            return(false);
        }