如何保存在数据库中的文件路径? [英] How to save a file path at database?

查看:256
本文介绍了如何保存在数据库中的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做音乐上传到我的应用程序,使用uploadify插件,上传正在努力,可是,当我会保存路径参考(其中音乐保存在我的项目),我得到空价值,任何人都知道我该怎么做呢?

i'm doing a music upload to my application, using the uploadify plugin, the upload are working, but, when i'll save the path reference (where the music are saved on my project), i'm getting null value, anyone knows how can I do this?

在MusicController上传方法

public ActionResult Upload()
{
    var file = Request.Files["Filedata"];
    string savePath = Server.MapPath(@"~\mp3\" + file.FileName);
    file.SaveAs(savePath);
    return Content(Url.Content(@"~\mp3\" + file.FileName));
}

在MusicController创建方法

  public ActionResult Create(Musica musica)
        {
            MembershipUser user = Membership.GetUser();
            int userId = Int32.Parse(Membership.GetUser().ProviderUserKey.ToString());
            string userName = user.UserName;

            musica.UserId = userId;
            musica.NomeArtista = userName;

            if (musica.isFree)
            {

                musica.Preco = 0;
            }

            //Here I try to take the path value
            musica.path = Upload().ToString();

            if (ModelState.IsValid)
            {
                db.Musicas.Add(musica);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.GeneroId = new SelectList(db.Generos, "GeneroId", "Nome", musica.GeneroId);
            return View(musica);
        }

在这种情况下,我只希望在我的数据库中保存,这样的信息:

On this case, I only want to save in my database, this information:

~\mp3\MusicExample.mp3

有人能帮助我吗?

Someone can help me?

推荐答案

编辑:所以,你正在使用的上传方法是异步的,并且它上传文件,并返回文件路径回页。更改上传方式来返回你想要的文件路径,捕捉它在jQuery和创建一个保存它的上传后,信息的控制:

So the upload method you're using is asynchronous, and it uploads the file and returns the file path back to the page. Change the upload method to return the file path you want, capture it in the jQuery and create a control that holds the information after it's uploaded:

您上传的方法应该是这样的:

Your upload method should be this:

public ActionResult Upload()
{
    var file = Request.Files["Filedata"];
    string savePath = Server.MapPath(@"~\mp3\" + file.FileName);
    file.SaveAs(savePath);
    return Content(@"~\mp3\" + file.FileName);
}

Somwhere在你看来,增加一个控制在路径属性:

@Html.EditorFor(model => model.path, new { id = "path"})

在javascript中,获取返回值,并将其设置为路径的新文本框里面在MUSICA属性:

In the javascript, capture the return value and set it inside the new textbox for the path property in musica:

'onUploadSuccess': function (file, data, response) {
                $("#path").val(data);
            }

在创建方法,去除code调用上传

In the create method, remove the code to call Upload:

//Stuff above here
if (musica.isFree)
{
    musica.Preco = 0;
}

//Here I try to take the path value
//musica.path = Upload().ToString();

if (ModelState.IsValid)
//stuff below here

现在,当你点击创建按钮,它会自动设置path属性的文件路径,它应该只是工作

Now, when you click on the create button, it should automatically set the path property to the file path, and it should just work

这篇关于如何保存在数据库中的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆