这是我的注册控制器,我希望用户上传他的个人资料图片,然后在他登录时必须显示。
public ActionResult Register(REGISTRATION registration)
{
var email = registration.Email;
var username = registration.UserName;
var pass = registration.Password;
var confirmpass = registration.ConfirmPassword;
var mobile = registration.MobileNumber;
byte image = Convert.ToByte(registration.Image);
OracleConnection connection = new OracleConnection();
connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionDHC"].ToString();
connection.Open();
OracleCommand command = connection.CreateCommand();
string query = "INSERT INTO REGISTRATION (EMAIL,USERNAME,PASSWORD,CONFIRMPASSWORD,MOBILENUMBER,IMAGE) VALUES('" + email + "','" + username + "','" + pass + "','" + confirmpass + "','" + mobile + "','" + image + "')";
command.CommandText = query;
if (MailExists(registration))
{
return View("MailExists");
}
command.ExecuteNonQuery();
return View("Congratulation");
}
我必须补充一点,它必须保存图像路径。??
答案 0 :(得分:0)
您是从fileupload获取图像输入,还是存储文件类型(.jpeg / .jpg / .png),这将帮助您在View上显示图像。
在视图中添加文件输入以拍摄照片
@using (Html.BeginForm("Index", "Default4", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input id="File1" name="File1" type="file" />
<input id="Submit1" type="submit" value="submit" />
}
在控制器中获取已发布的文件
public ActionResult Register(REGISTRATION registration)
{
var email = registration.Email;
var username = registration.UserName;
var pass = registration.Password;
var confirmpass = registration.ConfirmPassword;
var mobile = registration.MobileNumber;
string base64 = string.Empty;
// Getting Posted File
if (Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength > 0)
{
Stream stream = file.InputStream;
byte[] byteArray = ReadFully(stream);
base64 = Convert.ToBase64String(byteArray);
}
}
byte image = Convert.ToByte(registration.Image);
OracleConnection connection = new OracleConnection();
connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionDHC"].ToString();
connection.Open();
OracleCommand command = connection.CreateCommand();
string query = @"INSERT INTO REGISTRATION (EMAIL,USERNAME,PASSWORD,CONFIRMPASSWORD,MOBILENUMBER,IMAGE) VALUES
('" + email + "','" + username + "','" + pass + "','" + confirmpass + "','" + mobile + "','" + base64 + "')";
command.CommandText = query;
if (MailExists(registration))
{
return View("MailExists");
}
command.ExecuteNonQuery();
return View("Congratulation");
}
要在视图上显示
@model Application.Models.Registration
@if (registration.Image != null)
{
var base64 = registration.Image;
var imgSrc = String.Format("data:image/jpeg;base64,{0}", base64);
<img src="@imgSrc" alt="Image" width="256px" height="64px" />
}