MVC让MP3和图像列表到表 [英] MVC getting a list of mp3 and image into a table
问题描述
大家好我在codeS已经插上传文件和它的工作,但问题是我要上传图片是在图像名称表,音乐,音乐表列,还有一件事是我没有得到从任何数据库只是目录文件夹就可以了。
我的继承人剃刀HTML我的所有文件都上传到一个目录中,有一个code得到的.jpg成图像和明杆MP3到MP3柱的延伸?或者反正是有编码它
<表>
@foreach(在Directory.GetFiles变种F(使用Server.Mappath(@ ViewBag.UploadURL)))
{
VAR的fileInfo =新的FileInfo(F); ViewBag.FileNoExtension = fileInfo.Name.Substring(0,fileInfo.Name.IndexOf()'。');
ViewBag.FileExtension = fileInfo.Name.Substring(fileInfo.Name.IndexOf()+1'。');
&所述; TR>
< TD风格=宽度=10> @ fileInfo.Name< / TD>
@ *< TD> @ ViewBag.FileNoExtension< / TD> * @
&所述; TD>&下; IMG宽度=50高度=50SRC =@ Url.Content(@ ViewBag.UploadURL +/+ fileInfo.Name)/>&下; / TD>
&所述; TD>
@foreach(在Directory.GetFiles风险价值(使用Server.Mappath(@ ViewBag.MusicURL)))
{VAR fileInfoc =新的FileInfo(一);
//ViewBag.FileNoExtension = fileInfoc.Name.Substring(0,fileInfoc.Name.IndexOf()'。');
ViewBag.FileExtensionc = fileInfoc.Name.Substring(fileInfoc.Name.IndexOf()+1'。'); @ fileInfoc.Name
} < / TD>
}
&所述; TD>
@ Html.ActionLink(删除,删除,新{ID = @ ViewBag.StoryID,ID2 = @ ViewBag.FileNoExtension,ID3 = @ ViewBag.FileExtension})
< / TD> < / TR>
}< /表>
确定继承人我的控制器
[HttpPost]
公众的ActionResult上传(INT ID,IEnumerable的< HttpPostedFileBase> ImageInput,IEnumerable的< HttpPostedFileBase> MP3Input)
{
剧情故事= db.stories.Find(ID);
ViewBag.StoryID = story.StoryID; ViewBag.MusicURL =〜/上传/剧情/音乐+/+ story.FileURL;
ViewBag.ImageURL =〜/上传/剧情/图片+/+ story.FileURL; CreateDirectory(〜/上传/剧情/音乐+/+ story.FileURL);
CreateDirectory(〜/上传/剧情/图片+/+ story.FileURL);
字符串filepathMusic =〜/上传/剧情/音乐+/+ story.FileURL;
字符串filepathImage =〜/上传/剧情/图片+/+ story.FileURL; //获取仅适用于图像的jpg文件
的foreach(在ImageInput VAR镜像文件)
{
如果(镜像文件!= NULL)
{
串延长= Path.GetExtension(imagefile.FileName);
变种文件名= imagefile.FileName;
VAR的ImagePath = Path.Combine(使用Server.Mappath(filepathImage),文件名);
开关(扩展)
{
//案.MP3:
案.JPG:
变种F = Directory.GetFiles(使用Server.Mappath(filepathImage)); VAR UPLOADEDFILES =新的List<串GT;();
var中的文件= Directory.GetFiles(使用Server.Mappath(filepathImage));
的foreach(在文件VAR文件2)
{
VAR的fileInfo =新的FileInfo(文件2);
uploadedFiles.Add(fileInfo.Name);
ViewBag.b =的fileInfo;
}
imagefile.SaveAs(的ImagePath); //上传文件,因为它是一个形象
打破;
默认:
//不是一个图像 - 忽略
打破;
}
//返回视图(上传); //file.SaveAs(imagePath);
}
}
//获取唯一的音乐文件 的foreach(在MP3Input VAR Musicfile)
{
如果(Musicfile!= NULL)
{
串延长= Path.GetExtension(Musicfile.FileName);
VAR filemusic = Musicfile.FileName;
VAR MusicPath = Path.Combine(使用Server.Mappath(filepathMusic),filemusic);
开关(扩展)
{
案.MP3:
VAR一个= Directory.GetFiles(使用Server.Mappath(filepathMusic)); VAR UPLOADEDFILES =新的List<串GT;();
变种filesc = Directory.GetFiles(使用Server.Mappath(filepathMusic)); 的foreach(在filesc VAR文件3)
{
VAR fileInfoc =新的FileInfo(文件3);
uploadedFiles.Add(fileInfoc.Name);
ViewBag.a = fileInfoc;
}
Musicfile.SaveAs(MusicPath);
//案.JPG:
//上传文件,因为它是一个MP3
打破;
默认:
//不是一个MP3 - 忽略
打破;
}
//file.SaveAs(imagePath);
}
}
我假设你有某种形式的共同命名的图像涉及到的名称。在这个例子中我只是分配图像的MP3文件按顺序排列。
- 请在控制器,而不是你鉴于杂乱的工作。保持简单的看法。
- 使用视图模型更接近你的看法有什么需要的内容。
- 获取控制器做原料转化成事实视图模型的杂乱的工作。
- 控制器然后通过视图模式来查看显示它。
您的看法应该是简单的是这样的:
@model IEnumerable的< SomeWebApplication.Models.MusicItem>
<表类=显示ID =榜样>
<&THEAD GT;
&所述; TR>
<第i个图像文件< /第i
<第i个MP3文件< /第i
<第i删除< /第i
< / TR>
< / THEAD>
<&TBODY GT;
@foreach(以型号VAR项)
{
&所述; TR>
&所述; TD>&下; IMG宽度=50高度=50SRC =@ item.ImageUrl/>&下; / TD>
< TD><嵌入SRC =@ item.MusicUrl/>< / TD>
< TD> @ Html.ActionLink(删除,删除,新{ID = @ item.Id})LT; / TD>
< / TR>
}
< / TBODY>
< /表>
...它需要MusicItems作为视图模型的集合
视图模型将如下:
公共类MusicItem
{
///<总结>
///项的唯一ID
///< /总结>
公众诠释标识{搞定;组; } ///<总结>
/// URL图像
///< /总结>
公共字符串的ImageUrl {搞定;组; } ///<总结>
///网址音乐
///< /总结>
公共字符串MusicUrl {搞定;组; }
}
您的控制器应做繁重:
公共ActionResult的详细信息(INT ID)
{
剧情故事= db.Stories.Find(ID); //使用服务器的观点相对路径
字符串filepathMusic =/上传/剧情/音乐/+ story.FileURL;
字符串filepathImage =/上传/剧情/图片/+ story.FileURL; //建立MusicItem对象的列表
清单< MusicItem>项目=新的List< MusicItem>();
字符串[] = musicFiles Directory.GetFiles(使用Server.Mappath(〜+ filepathMusic));
的foreach(在musicFiles VAR musicFile)
{
items.Add(新MusicItem()
{
ID = ID,
MusicUrl = filepathMusic +/+ Path.GetFileName(musicFile)
});
} //这个例子只是简单的分配顺序找到的图像 - 需要正确地做到这一点
字符串[]的图像文件= Directory.GetFiles(使用Server.Mappath(〜+ filepathImage));
INT索引= 0;
的foreach(以图像文件中VAR镜像文件)
{
如果(指数< items.Count)
{
项目[指数] .ImageUrl = filepathImage +/+ Path.GetFileName(镜像文件);
}
指数++;
} 返回查看(项目);
}
那么,假设你有2个图像和两个文件在你的故事......
测试code的最终结果上面看起来是这样的:
hi guys i have already plug in the codes for uploading files and its working but the problem is i want upload image to be in image name table , music in music table column , and one more thing is i not getting from any database just directory folder will do.
heres my razor html all my files are upload into a directory, is there a code to get the extension of .jpg into image coloum and mp3 to mp3 column? or is there anyway of coding it
<table>
@foreach (var f in Directory.GetFiles(Server.MapPath(@ViewBag.UploadURL)))
{
var fileInfo = new FileInfo(f);
ViewBag.FileNoExtension = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
ViewBag.FileExtension = fileInfo.Name.Substring(fileInfo.Name.IndexOf('.') + 1);
<tr>
<td style = width = "10">@fileInfo.Name</td>
@* <td>@ViewBag.FileNoExtension</td>*@
<td><img width="50" height="50" src="@Url.Content(@ViewBag.UploadURL + "/" + fileInfo.Name)" /></td>
<td>
@foreach(var a in Directory.GetFiles(Server.MapPath(@ViewBag.MusicURL)))
{ var fileInfoc = new FileInfo(a);
//ViewBag.FileNoExtension = fileInfoc.Name.Substring(0, fileInfoc.Name.IndexOf('.'));
ViewBag.FileExtensionc = fileInfoc.Name.Substring(fileInfoc.Name.IndexOf('.') + 1);
@fileInfoc.Name
}
</td>
}
<td>
@Html.ActionLink("Remove", "Remove", new { id = @ViewBag.StoryID, id2 = @ViewBag.FileNoExtension, id3 = @ViewBag.FileExtension })
</td>
</tr>
} </table>
ok heres my controller
[HttpPost]
public ActionResult Upload(int id, IEnumerable<HttpPostedFileBase> ImageInput,IEnumerable<HttpPostedFileBase> MP3Input)
{
story story = db.stories.Find(id);
ViewBag.StoryID = story.StoryID;
ViewBag.MusicURL = "~/Upload/story/Music" + "/" + story.FileURL;
ViewBag.ImageURL = "~/Upload/story/Image" + "/" + story.FileURL;
CreateDirectory("~/Upload/story/Music" + "/" + story.FileURL);
CreateDirectory("~/Upload/story/Image" + "/" + story.FileURL);
String filepathMusic = "~/Upload/story/Music" + "/" + story.FileURL;
String filepathImage = "~/Upload/story/Image" + "/" + story.FileURL;
// get images jpg file only
foreach (var imagefile in ImageInput)
{
if (imagefile != null)
{
string extension = Path.GetExtension(imagefile.FileName);
var fileName = imagefile.FileName;
var imagePath = Path.Combine(Server.MapPath(filepathImage), fileName);
switch (extension)
{
// case ".mp3":
case ".jpg":
var f = Directory.GetFiles(Server.MapPath(filepathImage));
var uploadedFiles = new List<String>();
var files = Directory.GetFiles(Server.MapPath(filepathImage));
foreach(var file2 in files)
{
var fileInfo = new FileInfo(file2);
uploadedFiles.Add(fileInfo.Name);
ViewBag.b = fileInfo;
}
imagefile.SaveAs(imagePath);
//Upload file as it is an image
break;
default:
//Not an image - ignore
break;
}
// return View("Upload");
//file.SaveAs(imagePath);
}
}
// get music files only
foreach (var Musicfile in MP3Input)
{
if (Musicfile != null)
{
string extension = Path.GetExtension(Musicfile.FileName);
var filemusic = Musicfile.FileName;
var MusicPath = Path.Combine(Server.MapPath(filepathMusic), filemusic);
switch (extension)
{
case ".mp3":
var a = Directory.GetFiles(Server.MapPath(filepathMusic));
var uploadedFiles = new List<String>();
var filesc = Directory.GetFiles(Server.MapPath(filepathMusic));
foreach (var file3 in filesc)
{
var fileInfoc = new FileInfo(file3);
uploadedFiles.Add(fileInfoc.Name);
ViewBag.a = fileInfoc;
}
Musicfile.SaveAs(MusicPath);
// case ".jpg":
//Upload file as it is an mp3
break;
default:
//Not an mp3 - ignore
break;
}
//file.SaveAs(imagePath);
}
}
I have assumed you have some form of common naming to relate the images to the names. For this example I have simply assigned the images to the MP3 files in sequence.
- Do the messy work in the controller, not your view. Keep views simple.
- Use a View model for the content that is closer to what your view needs.
- Get the controller to do the messy work of converting raw facts into the view model.
- Controller then passes view mode to view to display it.
Your view should be simple like this:
@model IEnumerable<SomeWebApplication.Models.MusicItem>
<table class="display" id="example">
<thead>
<tr>
<th>Image File</th>
<th>MP3 File</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td><img width="50" height="50" src="@item.ImageUrl" /></td>
<td><embed src="@item.MusicUrl" /></td>
<td>@Html.ActionLink("Remove", "Remove", new { id = @item.Id })</td>
</tr>
}
</tbody>
</table>
...it takes a collection of MusicItems as its View Model
The View Model will look like:
public class MusicItem
{
/// <summary>
/// Unique id of item
/// </summary>
public int Id { get; set; }
/// <summary>
/// URL to image
/// </summary>
public string ImageUrl { get; set; }
/// <summary>
/// Url to music
/// </summary>
public string MusicUrl { get; set; }
}
Your controller should do the heavy lifting:
public ActionResult Details(int id)
{
Story story = db.Stories.Find(id);
// Use server relative paths for views
String filepathMusic = "/Upload/story/Music/" + story.FileURL;
String filepathImage = "/Upload/story/Image/" + story.FileURL;
// Build a list of MusicItem objects
List<MusicItem> items = new List<MusicItem>();
string[] musicFiles = Directory.GetFiles(Server.MapPath("~" + filepathMusic));
foreach (var musicFile in musicFiles)
{
items.Add(new MusicItem()
{
Id = id,
MusicUrl = filepathMusic + "/" + Path.GetFileName(musicFile)
});
}
// This example simply allocates the images in the order found - need to do this properly
string[] imageFiles = Directory.GetFiles(Server.MapPath("~" + filepathImage));
int index = 0;
foreach (var imageFile in imageFiles)
{
if (index < items.Count)
{
items[index].ImageUrl = filepathImage + "/" + Path.GetFileName(imageFile);
}
index++;
}
return View(items);
}
then, assuming you have 2 images and two files in your story...
The end result of the test code above looks like this:
这篇关于MVC让MP3和图像列表到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!