Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        //POST : api/Pictures
        public async Task <IHttpActionResult> Post()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new Exception_DG("unsupported media type", 2005);
            }
            CreateDirectoryIfNotExist(RootPathMvc + "/temp");
            var provider = new MultipartFormDataStreamProvider(RootPathMvc + "/temp");
            // Read the form data.
            await Request.Content.ReadAsMultipartAsync(provider);

            List <string> fileNameList  = new List <string>();
            StringBuilder sb            = new StringBuilder();
            long          fileTotalSize = 0;
            int           fileIndex     = 1;

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                //new folder
                string newRoot = RootPathMvc + @"Files/Pictures";
                CreateDirectoryIfNotExist(newRoot);
                if (File.Exists(file.LocalFileName))
                {
                    //new fileName
                    string fileName        = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2);
                    string newFileName     = Guid.NewGuid() + "." + fileName.Split('.')[1];
                    string newFullFileName = newRoot + "/" + newFileName;
                    fileNameList.Add($"Files/Pictures/{newFileName}");
                    FileInfo fileInfo = new FileInfo(file.LocalFileName);
                    fileTotalSize += fileInfo.Length;
                    sb.Append($" #{fileIndex} Uploaded file: {newFileName} ({ fileInfo.Length} bytes)");
                    fileIndex++;
                    File.Move(file.LocalFileName, newFullFileName);
                    Trace.WriteLine("1 file copied , filePath=" + newFullFileName);
                }
            }
            return(Json(Return_Helper.Success_Msg_Data_DCount_HttpCode($"{fileNameList.Count} file(s) /{fileTotalSize} bytes uploaded successfully!   Details -> {sb.ToString()}", fileNameList, fileNameList.Count)));
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        //POST : api/Files
        public async Task <IHttpActionResult> Post()
        {
            //get server root physical path
            string root = IO_Helper.RootPath_MVC;
            //new folder
            string newRoot = root + @"Files/Files/";

            //check path is exist if not create it
            IO_Helper.CreateDirectoryIfNotExist(newRoot);
            List <string> fileNameList  = new List <string>();
            StringBuilder sb            = new StringBuilder();
            long          fileTotalSize = 0;
            int           fileIndex     = 1;
            //get files from request
            HttpFileCollection files = HttpContext.Current.Request.Files;
            await Task.Run(() =>
            {
                foreach (var f in files.AllKeys)
                {
                    HttpPostedFile file = files[f];
                    if (!string.IsNullOrEmpty(file.FileName))
                    {
                        string fileLocalFullName = newRoot + file.FileName;
                        file.SaveAs(fileLocalFullName);
                        fileNameList.Add($"Files/Files/{file.FileName}");
                        FileInfo fileInfo = new FileInfo(fileLocalFullName);
                        fileTotalSize    += fileInfo.Length;
                        sb.Append($" #{fileIndex} Uploaded file: {file.FileName} ({ fileInfo.Length} bytes)");
                        fileIndex++;
                        Trace.WriteLine("1 file copied , filePath=" + fileLocalFullName);
                    }
                }
            });

            return(Json(Return_Helper.Success_Msg_Data_DCount_HttpCode($"{fileNameList.Count} file(s) /{fileTotalSize} bytes uploaded successfully!   Details -> {sb.ToString()}", fileNameList, fileNameList.Count)));
        }