如何保存在数据库中的文件路径?

编程入门 行业动态 更新时间:2024-10-27 04:24:11
本文介绍了如何保存在数据库中的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在做音乐上传到我的应用程序,使用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

更多推荐

如何保存在数据库中的文件路径?

本文发布于:2023-11-07 01:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   路径   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!