如何在asp.net mvc3中使用部分页面上传文件 [英] How to use file upload from a partial page in asp.net mvc3
问题描述
我有一个局部视图页面,其中我有文件上传标记。我的部分视图页面下方有一个剃刀视图
@using( Html.BeginForm(上传,提供者,FormMethod.Post,新{id =form,enctype =multipart / form-data}))
{
选择图片
< input type =filename =file/>
< input type =submitvalue =上传图片name =Command/>
}
我的控制器方法为
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
// 仅提取fielname
var fileName = Path.GetFileName (file.FileName);
// 将文件存储在〜/ images / User-Image文件夹
var path = Path.Combine(Server.MapPath( 〜/ images /),fileName);
// 这是您必须在数据库中保存的字符串
< span class =code-keyword> string filepathToSave = images / + fileName ;
file.SaveAs(path);
// -------------------- ------------------------
// 通过传递文件路径在DB中保存文件的代码
// --------------------------------------- -------
return View();
}
我总是在控制器方法的变量file中得到一个空值。我做了一点研究this.Concluded,正常情况下,如果我们从普通视图而不是部分视图使用文件上传它工作正常。但是从部分视图页面意外地上传文件。它像往常一样点击控制器方法,但每次我得到null我在前面提到的变量文件中的值。我搜索了谷歌以获得此问题的解决方案。但是失败了。请帮帮我。
< pre lang =cs> // 上传图片并保存图片上传文件夹
使用 System.IO;
{
OpenFileDialog op1 = new OpenFileDialog();
op1.Multiselect = true ;
op1.ShowDialog();
op1.Filter = allfiles | * .xls;
textBox1.Text = op1.FileName;
pictureBox1.ImageLocation = op1.FileName;
int count = 0 ;
string [] FName;
foreach ( string s in op1.FileNames)
{
FName = s.Split(' \\' 跨度>);
File.Copy(s, Path + FName [FName.Length - 1 跨度>]);
count ++;
}
MessageBox.Show(Convert.ToString(count)+ Image Uploaded跨度>);
}
它可以帮助你.. !!
I have a partial view page,in which i have markup for file upload.I have a razor view as below inside my partial view page
@using (Html.BeginForm("Upload", "Provider", FormMethod.Post, new { id = "form", enctype = "multipart/form-data" }))
{
Select Image
<input type="file" name="file" />
<input type="submit" value="Upload Image" name="Command" />
}
My controller method goes as
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/images/User-Image folder
var path = Path.Combine(Server.MapPath("~/images/"), fileName);
// this is the string you have to save in your DB
string filepathToSave = "images/" + fileName;
file.SaveAs(path);
//--------------------------------------------
// Code to save file in DB by passing the file path
//----------------------------------------------
return View();
}
I always get a null value in variable "file" of controller method.I did a little bit research over this.Concluded that, in normal way if we are using file upload from a normal view instead of partial view it work's fine.But file upload work unexpectedly from a partial view page.It hits controller method as usual,but every time i get null value in variable "file" as i have mentioned earlier.I searched over google to get a solution of this issue.But failed.Please help me out.
// Upload image And save Image At Upload Folder using System.IO; { OpenFileDialog op1 = new OpenFileDialog(); op1.Multiselect = true; op1.ShowDialog(); op1.Filter = "allfiles|*.xls"; textBox1.Text = op1.FileName; pictureBox1.ImageLocation = op1.FileName; int count = 0; string[] FName; foreach (string s in op1.FileNames) { FName = s.Split('\\'); File.Copy(s, "Path" + FName[FName.Length - 1]); count++; } MessageBox.Show(Convert.ToString(count) + " Image Uploaded"); }
it may help u ..!!
这篇关于如何在asp.net mvc3中使用部分页面上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!