コード例 #1
0
        public async Task <ActionResult> LoadFTPFiles(FTPCredentials credentials, Guid requestId, IEnumerable <string> paths)
        {
            List <Document> uploadedDocuments = new List <Document>();

            using (var sftp = new SftpClient(credentials.Address, credentials.Port, credentials.Login, credentials.Password))
            {
                try
                {
                    sftp.Connect();
                    foreach (var path in paths)
                    {
                        var fileInfo = sftp.Get(path);
                        using (var sSource = sftp.OpenRead(path))
                        {
                            using (var db = new DataContext())
                            {
                                var document = new Document
                                {
                                    CreatedOn = DateTime.UtcNow,
                                    FileName  = fileInfo.Name,
                                    ItemID    = requestId,
                                    Length    = fileInfo.Length,
                                    MimeType  = FileEx.GetMimeTypeByExtension(fileInfo.Name),
                                    Name      = fileInfo.Name,
                                    Viewable  = false,
                                    Kind      = DocumentKind.User
                                };

                                db.Documents.Add(document);
                                uploadedDocuments.Add(document);

                                await db.SaveChangesAsync();

                                var docStream = new DocumentStream(db, document.ID);

                                await sSource.CopyToAsync(docStream);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, ex.Message));
                }
                finally
                {
                    if (sftp.IsConnected)
                    {
                        sftp.Disconnect();
                    }
                }
            }

            Response.StatusCode  = (int)HttpStatusCode.OK;
            Response.ContentType = "application/json";

            return(Json(uploadedDocuments.Select(d => new { d.ID, d.FileName, d.MimeType, Size = d.Length }), JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
        public async Task <ActionResult> UploadFiles(Guid requestID)
        {
            if (Request.Files.Count != 1)
            {
                throw new ArgumentOutOfRangeException("Only a single file may be uploaded at a time.");
            }
            if (Request.InputStream.Length == 0)
            {
                throw new ArgumentOutOfRangeException("The stream is 0 length and cannot be saved.");
            }

            try
            {
                if (Request.InputStream.Length != 0)
                {
                    var             file   = Request.Files[0];
                    MultipartParser parser = new MultipartParser(Request.InputStream);
                    if (!parser.Success)
                    {
                        throw new ArgumentException("Unable to parse the file content correctly.");
                    }

                    List <Document> addedDocuments = new List <Document>();
                    using (var db = new DataContext())
                    {
                        string filename = System.IO.Path.GetFileName(file.FileName);
                        var    document = new Document
                        {
                            CreatedOn = DateTime.UtcNow,
                            FileName  = filename,
                            ItemID    = requestID,
                            Length    = parser.FileContents.LongLength,
                            MimeType  = FileEx.GetMimeTypeByExtension(filename),
                            Name      = System.IO.Path.GetFileName(filename),
                            Viewable  = false,
                            Kind      = DocumentKind.User
                        };

                        db.Documents.Add(document);
                        addedDocuments.Add(document);

                        await db.SaveChangesAsync();

                        var docStream = new DocumentStream(db, document.ID);
                        await new MemoryStream(parser.FileContents).CopyToAsync(docStream);
                    }

                    return(Json(addedDocuments.Select(d => new { d.ID, d.FileName, d.MimeType, Size = d.Length }), JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e);
                throw e;
            }
            return(null);
        }