Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC 4 C#HttpPostedFileBase,如何存储文件_C#_Asp.net Mvc_Asp.net Mvc 3_Asp.net Mvc 4_Httppostedfilebase - Fatal编程技术网

ASP.NET MVC 4 C#HttpPostedFileBase,如何存储文件

ASP.NET MVC 4 C#HttpPostedFileBase,如何存储文件,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,httppostedfilebase,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Httppostedfilebase,型号 public partial class Assignment { public Assignment() { this.CourseAvailables = new HashSet<CourseAvailable>(); } public string AssignmentID { get; set; } public Nullable<System.DateTime> SubmissionDate { g

型号

public partial class Assignment
{
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }

    public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation  { get; set; }
    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}}
查看

<div class="editor-field">
    <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
</div>

model.FileLocation,新的{type=“file”})%>
model.FileLocation)%%>

如果我想将文件存储到服务器/路径文件夹中,而在数据库中我只想存储路径名/字符串,那么如何存储文件。

您可以上传文件并将其url保存在数据库表中,如下所示:

视图: 细节: 为了更好地理解,请参考以下内容:

View.cs

<div class="row">

    @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="FileUpload" id="FileUpload" runat="server" />
        <input type="submit" value="Save" class="btn btn-default" />
    }

</div>
附加模型

ViewDataUploadFilesResult.cs

public class ViewDataUploadFilesResult
{
    public string Name { get; set; }
    public string FilePath { get; set; }
    public int Length { get; set; }
    public HttpPostedFileBase FileObj { get; set; }
    public byte[] Content { get; set; }
}
对我来说,这不是使用整个ViewModel,而是我的附件表的实际模型:

public partial class ITEM_ATCHMT
{
    [Key]
    public Guid ATCHMT_ID { get; set; }

    public int ITEM_ID { get; set; }

    [ForeignKey("ITEM_ID")]
    public virtual ITEM item { get; set; }

    [Required]
    [StringLength(50)]
    public string USER_NAME_DESC { get; set; }

    [Required]
    [StringLength(250)]
    public string FILE_NAME_TXT { get; set; }

    [Required]
    public byte[] FILE_CNTNT_CD { get; set; }

    [Required]
    [StringLength(10)]
    public string FILE_TYPE_DESC { get; set; }

    public DateTime CREATED_DT { get; set; }
} 
我想把它和这个项目联系起来:

public partial class ITEM
{
    [Key]
    public int ITEM_ID { get; set; }

    [Required]
    [StringLength(50)]
    public string NAME { get; set; }

}
要使用Entity Framework保存任何数据,只需填充该模型,然后在上下文中执行
.SaveChanges()

EFModel db = new EFModel();  // this is my Entity Framework context
ITEM item = new ITEM();
item.NAME = "My Item";

db.ITEM.Add(item);  //"ITEM" is my table and name of an EF model, "item" is the object that represents my model
db.SaveChanges();
如果
ITEM_ID
设置为自动递增:

ITEM_ATCHMT atchmt_model = new ITEM_ATCHMT();
atchmt_model.ATCHMT_ID = Guid.NewGuid();
atchmt_model.ITEM_ID = item.ITEM_ID // <-- this should have the ID
atchmt_model.USER_NAME_DESC = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
atchmt_model.FILE_CNTNT_CD = r.Content;
atchmt_model.FILE_NAME_TXT = r.Name;
atchmt_model.FILE_TYPE_DESC = r.Name.Split('.')[1];
atchmt_model.CREATED_DT = DateTime.Now;

db.ITEM_ATCHMT.Add(atchmt_model);  //"ITEM_ATCHMT" is my table
db.SaveChanges();
ITEM_ATCHMT ATCHMT_model=new ITEM_ATCHMT();
atchmt_model.atchmt_ID=Guid.NewGuid();
atchmt_model.ITEM_ID=ITEM.ITEM_ID//
List svaVozila=new List();
使用(StreamReader sr=new StreamReader(@“C:\proba\mvcapapplication1\mvcapapplication1\fajlovi\vozila.txt”))
{
而(sr.Peek()>=0)
{
字符串str;
字符串[]strArray;
str=sr.ReadLine();
strArray=str.Split(“|”);
Vozila auto=新的Vozila();
auto.registicija=strArray[0];
auto.Marka=strArray[1];
auto.GodinaProiz=strArray[2];
auto.Boja=strArray[3];
svaVozila.Add(自动);
}
}
字符串registicija=Request.Form[“registicija”];
字符串数据=请求。表格[“数据”];
字符串odM=请求[“odMesta”];
字符串doM=Request.Form[“doMesta”];
字符串kmara=Request.Form[“kmara”];
如果(!String.IsNullOrEmpty(registracija))
{
使用(StreamWriter wr=new StreamWriter(@“C:\proba\mvcapapplication1\mvcapapplication1\fajlovi\”+registracija+“.txt”,true))
{
wr.WriteLine(注册号+“|”+“|”+数据+“|”+odM+“|”+doM+“|”+km);
}
}
返回视图(svaVozila);
}
公共行动结果Prikaz()
{
List svaVozila=新列表();
使用(StreamReader sr=new StreamReader(@“C:\proba\mvcapapplication1\mvcapapplication1\fajlovi\vozila.txt”))
{
而(sr.Peek()>=0)
{
字符串str;
字符串[]strArray;
str=sr.ReadLine();
strArray=str.Split(“|”);
Vozila auto=新的Vozila();
auto.registicija=strArray[0];
auto.Marka=strArray[1];
auto.GodinaProiz=strArray[2];
auto.Boja=strArray[3];
svaVozila.Add(自动);
}
}
字符串reg=请求[“reg”];
字符串Marka=“”;
字符串godia=“”;
int-ri=0;
for(int i=0;i=0)
{
字符串str;
字符串[]strArray;
str=sr.ReadLine();
strArray=str.Split(“|”);
PredjeniPut put=新的PredjeniPut();
put.registicija=strArray[0];
put.Datum=strArray[1];
put.Odmesta=strArray[2];
put.Domesta=strArray[3];
put.km=转换为32(strArray[4]);
predj.添加(put);
}
}
对于(int i=0;i
基准面
Od mesta
Do mesta
公里数
} @ActionLink(“Prikaz”、“Prikaz”、“home”)*@ @*@模型列表 @{ ViewBag.Title=“Prikaz”; } 普里卡兹 @使用(Html.BeginForm(“Prikaz,home”)) { @foreach(模型中的var i) { @i、 登记处 } 登记处 戈迪纳普罗伊兹沃德涅 马尔卡 公里 @ViewData[“reg”] @ViewData[“Godina”] @ViewData[“标记”] @ViewData[“公里数”] }
*@
非常感谢,它很有帮助,对C#来说还是个新手,一个或多个实体的MVC验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。会发生什么样的错误?插入时会发生此错误;db.SaveChanges();验证失败
public partial class ITEM_ATCHMT
{
    [Key]
    public Guid ATCHMT_ID { get; set; }

    public int ITEM_ID { get; set; }

    [ForeignKey("ITEM_ID")]
    public virtual ITEM item { get; set; }

    [Required]
    [StringLength(50)]
    public string USER_NAME_DESC { get; set; }

    [Required]
    [StringLength(250)]
    public string FILE_NAME_TXT { get; set; }

    [Required]
    public byte[] FILE_CNTNT_CD { get; set; }

    [Required]
    [StringLength(10)]
    public string FILE_TYPE_DESC { get; set; }

    public DateTime CREATED_DT { get; set; }
} 
public partial class ITEM
{
    [Key]
    public int ITEM_ID { get; set; }

    [Required]
    [StringLength(50)]
    public string NAME { get; set; }

}
EFModel db = new EFModel();  // this is my Entity Framework context
ITEM item = new ITEM();
item.NAME = "My Item";

db.ITEM.Add(item);  //"ITEM" is my table and name of an EF model, "item" is the object that represents my model
db.SaveChanges();
ITEM_ATCHMT atchmt_model = new ITEM_ATCHMT();
atchmt_model.ATCHMT_ID = Guid.NewGuid();
atchmt_model.ITEM_ID = item.ITEM_ID // <-- this should have the ID
atchmt_model.USER_NAME_DESC = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
atchmt_model.FILE_CNTNT_CD = r.Content;
atchmt_model.FILE_NAME_TXT = r.Name;
atchmt_model.FILE_TYPE_DESC = r.Name.Split('.')[1];
atchmt_model.CREATED_DT = DateTime.Now;

db.ITEM_ATCHMT.Add(atchmt_model);  //"ITEM_ATCHMT" is my table
db.SaveChanges();