开发者

uploading files in ASP.NET MVC

开发者 https://www.devze.com 2023-01-12 16:17 出处:网络
When I select a file and submit the file for upload, I can\'t get the value of the File path in my Model. In the Controller it shows as null. What am I doing wrong?

When I select a file and submit the file for upload, I can't get the value of the File path in my Model. In the Controller it shows as null. What am I doing wrong?

View

<form method="post" action="/Account/Profile" enctype="multipart/form-data">
    <label>Load photo: </label>
    <input type="file" name="filePath" id="file" />
    <input type="submit" value="submit" />
</form>

Controller

开发者_StackOverflowpublic ActionResult Profile(ProfileModel model, FormCollection form)
{
    string path = Convert.ToString(model.FilePath);
    return View();
}

Model

public HttpPostedFileBase FilePath
{
    get
    {
        return _filePath;
    }
    set
    {
        _filePath = value;
    }
}

public bool UploadFile()
{
    if (FilePath != null)
    {
        var filename = Path.GetFileName(FilePath.FileName);
        FilePath.SaveAs(@"C:\" + filename);
        return true;
    }
    return false;
}


I don't think model binding works with HttpPostedFileBase...

It should work if you take it out of your ViewModel and do it like this:

public ActionResult Profile(HttpPostedFileBase filePath)
{
    string path = Convert.ToString(filePath);
    return View();
}

HTHs,
Charles

Ps. This post could help explain things: ASP.NET MVC posted file model binding when parameter is Model


I don't have VS to simulate your problem. So im not sure bout the answer.

Try this, might work

<input type="file" name="model.FilePath" id="file" />

If not work then, Try look it on your formcollection & HttpContext.Request.Files

It should be there.


Instead of using HttpPostedFileBase I would use IFormFile

public class ModelFile
{
     .....
     public string Path{ get; set; }
     public ICollection<IFormFile> Upload { get; set; }
}


public async Task<IActionResult> Index([Bind("Upload,Path")] ModelFile modelfile)
{
     ...
     msg = await storeFilesInServer(modelfile.Upload,modelfile.Path);
}


private async Task<Message> storeFilesInServer(ICollection<IFormFile> upload, string path)
        {
            Message msg = new Message();
            msg.Status = "OK";
            msg.Code = 100;
            msg.Text = "File uploaded successfully";
            msg.Error = "";
            string tempFileName = "";
            try
            {
                foreach (var file in upload)
                {
                    if (file.Length > 0)
                    {
                        string tempPath = path + @"\";
                        if (Request.Host.Value.Contains("localhost"))
                            tempPath = path + @"\"; 

                        using (var fileStream = new FileStream(tempPath + file.FileName, FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                            tempFileName = file.FileName;
                        }
                    }
                    msg.Text = tempFileName;
                }
            }
            catch (Exception ex)
            {
                msg.Error = ex.Message;
                msg.Status = "ERROR";
                msg.Code = 301;
                msg.Text = "There was an error storing the files, please contact support team";
            }

            return msg;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消