在部署c#mvc之后将文件上载到服务器时出错
本文关键字:服务器 出错 上载 文件 部署 c#mvc 之后 | 更新日期: 2024-09-25 04:23:36
我正在c#mvc中创建一个应用程序,用户可以在其中上传mp3文件,下面是我的上传控制器:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string x = Request["ss"];
song std = new song();
if (file != null && file.ContentLength > 0)
try
{
// string path = Path.Combine(Server.MapPath("~/Songs"), Path.GetFileName(file.FileName));
string path = "~/Songs/" + Path.GetFileName(file.FileName);
var allowedExtensions = new[] { ".mp3",".MP3" };
var extension = Path.GetExtension(file.FileName);
if (!allowedExtensions.Contains(extension))
{
ViewBag.Message = "Only .mp3 extension files are allowed!!";
}
else
{
file.SaveAs(Server.MapPath(@"~'Songs'" + file.FileName));
//file.SaveAs(path);
string[] arr = { };
arr = (file.FileName).Split('.');
std.name = arr[0];
if (x == null || x == "")
{
std.artist = "Not Mentioned";
}
else {
std.artist = x;
}
std.likes = "0";
std.dislikes = "0";
repo4.save4(std); //call the function..
ViewBag.Message = "Song uploaded successfully!! ";
}
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return View();
}
以下是我对index.cshtml:的看法
@using (Html.BeginForm("index", "Upload", FormMethod.Post, new { enctype ="multipart/form-data" }))
{
<label class="l" for="file">Upload Song:</label>
<input class="l" type="file" name="file" id="file" />
<div class="form-inline form-group">
<label class="l">Artist' Name : </label>
<input type="text" class="form-control" name="ss" maxlength="30" placeholder="Leave it blank if u want.." />
</div>
<input type="submit" class="btn btn-warning" value="Upload" />
<div class="text-danger">@ViewBag.Message</div><br /><div class="text-success">Search uploaded song <a href="/Reg/reg">HERE</a></div>
}
现在,我将mp3文件保存到名为Songs的文件夹中。。当我在VS上构建项目时,这段代码似乎运行得很好。但在AppHarbor上部署后,当我尝试上传mp3文件时,我不断收到以下错误:
错误:访问路径'D:''Users''apphb180c99d1921484''app_PublishedWebsites''WebMatrixWebsite''Songs''Wake"Me Up.mp3"被拒绝。
请提供任何帮助,我将不胜感激,因为我无法理解…谢谢!!
现在为我工作。。以防有人还在想怎么。。您需要将网络服务的权限授予存储文件的文件夹。。
在这种情况下:
歌曲->属性。。打开安全选项卡->编辑->添加。。
选择"位置"。。到您的电脑。然后在"输入要选择的对象名称"下写入"网络服务",然后检查名称并输入ok…
现在,通过选中其中一个复选框来授予它权限,您可能需要选择读/写。。
我选择了"完全控制",这对我来说非常有效……希望你们也能成功:)干杯。。