1

私は現在asp.netmvc3を研究しており、現在、ユーザーがファイルをフォルダーにアップロードできるアプリケーションを実装しようとしています。

これが私が持っている最初の実装であり、実際には正常に機能しています。コントローラーコードは次のとおりです。

   public class FileUploadController : Controller
    {
        //
        // GET: /FileUpload/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [ActionName("Upload")]
        public ActionResult Index(FormCollection form)
        {

            string upFolder = Server.MapPath("~/FileUploadFiles/");

            if(!Directory.Exists(upFolder))
            {
                Directory.CreateDirectory(upFolder);
            }
            HttpPostedFileBase photo = Request.Files["fileupload"];

            if (photo != null)
            {
                photo.SaveAs(upFolder+photo.FileName);
                return RedirectToAction("Index");
            }



            return View();
        }

    }

これが私が持っている別の実装です、「パス'UserUploads \Uploads\'へのアクセスが拒否されました」というエラーが発生します。アップロードを処理するユーティリティクラスは次のとおりです。

 public static class FileUploader
    {
        public static char DirSeparator = Path.DirectorySeparatorChar;
        public static string FilesPath = "UserUploads" + DirSeparator + "Uploads" + DirSeparator;

        public static string UploadFile(HttpPostedFileBase file)
        {
            //check if we have a file
            if(file == null)
            {
                return "";
            }

            //make sure the file has content
            if(!(file.ContentLength > 0 ))
            {
                return "";
            }

            string fileName = file.FileName;
            string fileExt = Path.GetExtension(file.FileName);

            //make sure we are able to determine a proper extension
            if(fileExt == null)
            {
                return "";
            }

            //check if directory does not exists
            if(!Directory.Exists(FilesPath))
            {
                Directory.CreateDirectory(FilesPath);
            }

            //set our full path for saving
            string path = FilesPath + DirSeparator + fileName;
            //Save the file
            file.SaveAs(Path.GetFullPath(path));

            //Return the filename
            return fileName;

        }

        public static void DeleteFile(string fileName)
        {
            //Don't do anything if there is no name
            if(fileName.Length > 0)
            {
                return;
            }

            //Set our full path for deleting
            string path = FilesPath + DirSeparator + fileName;

            //Check if our file exists
            if(File.Exists(Path.GetFullPath(path)))
            {
                File.Delete(Path.GetFullPath(path));
            }
        }

コントローラのコードは次のとおりです。

using MvcFileUpload.Utility;

namespace MvcFileUpload.Controllers
{
    public class UploadFilesController : Controller
    {
        //
        // GET: /UploadFiles/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ActionName("Upload")]
        public ActionResult Index(HttpPostedFileBase file)
        {

            FileUploader.UploadFile(file);
            return RedirectToAction("Index");
        }

    }
}
4

4 に答える 4

3

FilePathディレクトリはどこに作成されますか?ウェブサイトのルートフォルダ内?「UserUploads」フォルダーを手動で作成し、AppPool(Webアプリケーションを含む)がそこに書き込むためのアクセス許可を実行するアカウントに付与する必要があります。

于 2013-02-26T15:31:50.787 に答える
2

パスが正しくない可能性はありますか?動作する実装では、Server.MapPath()を使用してディレクトリの完全な物理パスを使用していますが、ユーティリティクラスには部分的なパスしかないことに気付きました。FilesPath変数にフルパスを割り当てようとするとどうなりますか?それでも問題が解決しない場合は、ProcMonを実行して、アクセス拒否エラーが生成されたときにファイルシステムで何が起こっているかについての詳細情報を取得することをお勧めします。

于 2013-02-26T15:37:49.497 に答える
0

私はそれを解決することができました

ユーティリティクラスを変更することにより:

 public static string FilesPath = HttpContext.Current.Server.MapPath("~\\UserUploads" + DirSeparator + "Uploads" + DirSeparator);

ソリューションを提供してくれたSir/Ma'amに感謝します。ありがとう++

于 2013-02-26T16:29:56.737 に答える
0

解決策のどれも私を助けませんでした。ディレクトリアクセス権を持つIISにユーザーを割り当てることを考えています。

于 2013-12-05T18:49:23.643 に答える