带有jtable文件上传的Mvc不会加载.mp3音频文件 [英] Mvc with jtable file upload doesn't load the .mp3 audio file
本文介绍了带有jtable文件上传的Mvc不会加载.mp3音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试上传.mp3文件,但它不接受编辑窗口中的文件
我创建的数据库edmx文件名为HuffetEntities
以下是我的代码建议任何人
我尝试了什么:
我试过以下代码任何一个建议什么是变化需要下面的代码
i也跟着链接http://www.jtable.org/
I am trying to upload .mp3 file but it not accepting file in edit window
and i was created database edmx file for the name HuffetEntities
below is my code suggest anyone
What I have tried:
I tried the below code any one suggest what is changes to need the below code
i was followed the link also http://www.jtable.org/
public ActionResult UploadAudiofiles(Album C, tbl_Save_Song tb, HttpPostedFileBase file)
{
var path = string.Empty;
string subPath = string.Empty;
var destinationPath = string.Empty;
var fname = string.Empty;
if (file.ContentLength > 0)
{
int MaxContentLength = 2048576000;
string[] AllowedFileExtension = new string[] { ".jpg" };
if (!AllowedFileExtension.Contains
(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
{
ModelState.AddModelError("File", "Please file of type:" + string.Join(",", AllowedFileExtension));
}
else if (file.ContentLength > MaxContentLength)
{
ModelState.AddModelError("File", "Your file is too large,maximum allowed size is:" + MaxContentLength + "MB");
}
else
{
var fileName = Path.GetFileName(file.FileName);
if (fileName != null)
{
subPath = "/Albums/" + C.AlbumID;
bool exists = Directory.Exists(Server.MapPath(subPath));
if (!exists)
subPath = "/Albums/" + C.AlbumID;
Directory.CreateDirectory(Server.MapPath(subPath));
}
// fname = subPath + "/" + fileName;
fileName = fileName.ToString();
// destinationPath = Path.Combine(Server.MapPath(subPath), fileName);
// file.SaveAs(destinationPath);
// file.SaveAs(fileName);
ModelState.Clear();
ViewBag.Message = "File uploaded Successfully. File path: /videos/" + fileName;
}
}
else if (file == null)
{
ModelState.AddModelError("CustomError", "Returns Null Data");
return View();
}
if (ModelState.IsValid)
{
var fileName = string.Empty;
try
{
using (HuffetEntities entities = new HuffetEntities())
{
entities.SP_Upload_Songs(tb.SongName, tb.Lyricist, tb.SongDuration, tb.SingerName, tb.Audiopath, fileName, tb.AlbumID, tb.Status);
entities.SaveChanges();
}
ModelState.Clear();
C = null;
ViewBag.Message = "Successfully Done";
TempData["Alert Message"] = "My Alert Message";
}
catch (Exception ex)
{
//throw (ex);
ex.Track();
// return RedirectToAction("Thankyou");
ViewBag.Message = "Error! Please try again";
}
HuffetEntities dbContext = new HuffetEntities();
items = dbContext.Albums.Select(c => new SelectListItem
{
Value = c.AlbumID.ToString(),
Text = c.AlbumName
});
ViewBag.RetrieveAlbum = items;
// return RedirectToAction("Thankyou");
}
TempData["Alert Message"] = "Navigate to Result View";
return View("Result");
}
推荐答案
我用Jtable上传了.mp3音频文件
以下代码更改已完成
[HttpPost]
public JsonResult UploadAudiofiles(Album C,tbl_Save_Song tb,HttpPostedFileBase file)
{
var path = string.Empty;
string subPath = string.Empty;
var destinationPath = string。清空;
var fname = string.Empty;
ActionResponse response = new ActionResponse();
HttpPostedFileBase postedFile = Request.Files [file ];
if(file.ContentLength> 0)
{
if(ModelState.IsValid )
{
if(System.IO.File.Exists(tb.SongName))
{
返回Json(jTable.FileExit);
}
var fileName = Path.GetFileName(file.FileName);
int MaxContentLength = 2048576000;
string [] AllowedFileExtension = new string [] {。mp3};
if( !AllowedFileExtension.Contains
(file.FileName.Substring(file.FileName.LastIndexOf('。'))))
{
ModelState .AddModelError(文件,请文件类型:+ string.Join(,,AllowedFileExtension));
}
else if(file.ContentLength > MaxContentLength)
{
ModelState.AddModelError(文件,您的文件太大,允许的最大大小为:+ MaxContentLength +MB);
}
其他
{
subPath =/ Albums /+ C.AlbumID;
bool exists = Directory.Exists(Server.MapPath(subPath));
if(!exists)
subPath =/ Albums /+ C.AlbumID;
Directory.CreateDirectory(Server.MapPath(subPath));
fileName = fileName.ToString();
response.Status = ActionResponse._SUCCESS;
response.Message = fileName;
ModelState.Clear();
ViewBag.Message =文件已成功上传。文件路径:/相册/+文件名;
}
}
}
else
{
ModelState.AddModelError(CustomError,Http发布的文件数据返回空数据);
}
返回Json(响应,JsonRequestBehavior.DenyGet);
}
I done the uploading the .mp3 audio file with Jtable
below code is changes was done
[HttpPost]
public JsonResult UploadAudiofiles(Album C, tbl_Save_Song tb, HttpPostedFileBase file)
{
var path = string.Empty;
string subPath = string.Empty;
var destinationPath = string.Empty;
var fname = string.Empty;
ActionResponse response = new ActionResponse();
HttpPostedFileBase postedFile = Request.Files["file"];
if (file.ContentLength > 0)
{
if (ModelState.IsValid)
{
if (System.IO.File.Exists(tb.SongName))
{
return Json(jTable.FileExit);
}
var fileName = Path.GetFileName(file.FileName);
int MaxContentLength = 2048576000;
string[] AllowedFileExtension = new string[] { ".mp3" };
if (!AllowedFileExtension.Contains
(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
{
ModelState.AddModelError("File", "Please file of type:" + string.Join(",", AllowedFileExtension));
}
else if (file.ContentLength > MaxContentLength)
{
ModelState.AddModelError("File", "Your file is too large,maximum allowed size is:" + MaxContentLength + "MB");
}
else
{
subPath = "/Albums/" + C.AlbumID;
bool exists = Directory.Exists(Server.MapPath(subPath));
if (!exists)
subPath = "/Albums/" + C.AlbumID;
Directory.CreateDirectory(Server.MapPath(subPath));
fileName = fileName.ToString();
response.Status = ActionResponse._SUCCESS;
response.Message = fileName;
ModelState.Clear();
ViewBag.Message = "File uploaded Successfully. File path: /Albums/" + fileName;
}
}
}
else
{
ModelState.AddModelError("CustomError", "Http posted file data returns null data");
}
return Json(response, JsonRequestBehavior.DenyGet);
}
这篇关于带有jtable文件上传的Mvc不会加载.mp3音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文