我有一个提供商托管应用程序(asp.net MVC),我正在尝试使用表单上传文件,但没有任何工作.HttpPostedFileBase始终为NULL。 请帮助。
这里是我正在使用的代码,
sysdate
答案 0 :(得分:0)
您可以使用HttpPostFileBase
数据类型
ClassModel:
public DateTime submission_deadline_date { get; set; }
public string period_covered { get; set; }
public HttpPostedFileBase File { get; set; }
public string fk_level_register { get; set; }
public DateTime date_modified { get; set; }
public string done_by { get; set; }
的Controler:
[HttpPost]
public ActionResult Create(Period collection)
{
string fpath = "";
try
{
if (Request.Files.Count > 0)
{
try
{
HttpPostedFileBase file = collection.File;
string fname;
// want to check extension then use this method
//if (CheckExtension(file.ContentType.ToLower()))
//{
// Checking for Internet Explorer
string extension = System.IO.Path.GetExtension(file.FileName);
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = Guid.NewGuid() + extension; //+ testfiles[testfiles.Length - 1];
}
else
{
fname = Guid.NewGuid() + extension; //+ file.FileName;
}
// Get the complete folder path and store the file inside it.
fname = System.IO.Path.Combine(Server.MapPath("~/Content/"), fname);
file.SaveAs(fname);
return Content("Successfully Uploaded");
//}
}
catch (Exception ex)
{
return Content("Error occurred. Error details: " + ex.Message);
}
}
}
catch (Exception)
{
return Content(Response.StatusCode.ToString() + " Bad Requrest error." + fpath);
}
return Content("No files selected.");
}
public bool CheckExtension(string Ext)
{
if (Ext == "application/pdf")
{
return true;
}
else if (Ext == "text/plain")
{
return true;
}
else if (Ext == "application/msword")
{
return true;
}
else if (Ext == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
return true;
}
else
{
return false;
}
}
查看:仅查看您需要做的小改动
@using (Html.BeginForm("Create", "Home", null, FormMethod.Post, new { id = "formU", enctype = "multipart/form-data" })){
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.fk_level_register)
@Html.HiddenFor(model => model.date_modified)
@Html.HiddenFor(model => model.done_by)
<label for="file">Filename:</label>
<input type="file" name="File" id="File" style="width:240px" />
@*@Html.EditorFor(model => model.File, new { htmlAttributes = new { @type="file", @class = "form-control", @placeholder = "File path" } })*@
<div class="form-group">
@Html.LabelFor(model => model.submission_deadline_date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.submission_deadline_date)
@Html.ValidationMessageFor(model => model.submission_deadline_date)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.period_covered, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.period_covered, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.period_covered)
</div>
</div>
<div class="form-group">
<input id="submit" type="submit" value="Submit" class="btn btn-success" />
</div>
</div>
</div>}
您还可以从git hub repository click here
下载源代码答案 1 :(得分:0)
由于您使用的是提供商托管应用,为何不使用此方法?
https://msdn.microsoft.com/en-us/library/office/dn904536.aspx