예제 #1
0
 public ActionResult UploadFiles(IEnumerable <HttpPostedFileBase> attachments)
 {
     // The Name of the Upload component is "attachments"
     foreach (var file in attachments)
     {
         // Some browsers send file names with full path. We only care about the file name.
         var fileName        = Path.GetFileName(file.FileName);
         var destinationPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
         file.SaveAs(destinationPath);
         UploadedFiles.Add(destinationPath);
     }
     // Return an empty string to signify success
     return(Content(""));
 }
예제 #2
0
        /// <summary>
        /// ファイルのアップロード
        /// </summary>
        /// <param name="srcPath">アップロード対象ファイル</param>
        private async Task UploadFileAsync(string srcPath)
        {
            string relPath = srcPath.Substring(BaseDir.Length + 1);
            string dstPath = Path.Combine(UploadInfo.Target.Folder, relPath);

            //	ディレクトリの場合
            if (Directory.Exists(srcPath))
            {
                await CreateDirectoryAsync(dstPath);

                return;
            }
            //	アップロード一覧はHashSetにしたので常にユニーク
            UploadedFiles.Add(relPath);
            bool doCopy    = true;
            bool existFile = File.Exists(dstPath);

            //	既存ファイルはタイムスタンプをコピー条件にして不用意に古いものがアップされないようにする
            if (existFile)
            {
                var result = await CompareFileTimeAsync(srcPath, dstPath);

                doCopy = result > 0;
            }
            if (doCopy)
            {
                var dstDir = Path.GetDirectoryName(dstPath);
                if (!Directory.Exists(dstDir))
                {
                    await CreateDirectoryAsync(dstDir);
                }
                var flagStr = (existFile) ? "Copy:" : "New:";
                await Pane.WriteLineAsync($"{flagStr} {relPath} -> {dstPath}");

#if EXEC_UPLOAD
                await Task.Run(() =>
                {
                    //	ファイルをコピーする。もし、リードオンリー属性がついていたら強制で排除する(VSS対応時の名残)
                    File.Copy(srcPath, dstPath, true);
                    FileAttributes fileAttr = File.GetAttributes(srcPath);
                    fileAttr &= ~FileAttributes.ReadOnly;
                    File.SetAttributes(dstPath, fileAttr);
                });
#endif
            }
        }