3个回答
2013-07-22
展开全部
我用的是MS SQL,你把数据库换一下就可以了.protected void btnOk_Click(object sender, EventArgs e)
{ try
{
int count;
SqlParameter[] parm = new SqlParameter[8];
string picPath = (string)ViewState["Pic"];
if (!string.IsNullOrEmpty(picPath))
{
DeleteFile(Server.MapPath("~/MusicAlbum") + picPath);
}
parm[0] = new SqlParameter("@RecordID", int.Parse(txtRecordID.Text));
string strPicFileName = string.Empty;
if (FileUploadU(MapPath("~/") + @"Res\MusicAlbum\", fuPic,out strPicFileName))
{
parm[1] = new SqlParameter("@Pic", @"\" + strPicFileName);
parm[2] = new SqlParameter("@Title", txtTitle.Text);
parm[3] = new SqlParameter("@SingerName", txtSingerName.Text);
parm[4] = new SqlParameter("@VisitorCount", txtVisitCount.Text);
parm[5] = new SqlParameter("@CreatePeople", txtCreateDate.Text);
parm[6] = new SqlParameter("@EditPeople", txtEditPeople.Text);
parm[7] = new SqlParameter("@EditDate", DateTime.Now); count = RunProcedure("MIndex2NAHotAlbumLarge_Update", parm, out count);
if(count>0)
{
Common.Show(this, "操作成功!");
}
else
{
Common.Show(this, "操作失败!");
}
}
else
{
Common.Show(this, "上传失败,请重新上传!");
}
}
catch (Exception ex)
{
string strErrorMsg = "操作出错,错误信息:" + ex.Message + " 出错位置: " + ex.StackTrace;
Common.Show(this, strErrorMsg.Replace("\r", "\\r").Replace("\n", "\\n"));
}} public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int result;
connection.Open();
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
connection.Close();
return result;
}
}private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command;
} private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
} /// <summary>
/// 输入完整的路径,进行删除文件操作(如果上传同一文件,则执行此方法)
/// </summary>
/// <param name="filePath"></param>
public static void DeleteFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
catch { }
} /// <summary>
/// 上传文件到服务器
/// </summary>
/// <param name="savePath"></param>
/// <param name="fileupload"></param>
/// <param name="result"></param>
/// <returns></returns>
public bool FileUploadU(string savePath, FileUpload fileupload, out string result)
{
//从web.config中读取上传路径 ConfigurationManager.ConnectionStrings["path"].ConnectionString
string path = "";
try
{
//获取文件后缀名
int last = fileupload.PostedFile.FileName.LastIndexOf(".");
//获取指定路径的文件名
string fileExtension = fileupload.PostedFile.FileName.Substring(last + 1);
if (Common.IsAllowedExtension(fileupload) == true)
{
//上传文件后缀名
fileExtension = fileupload.PostedFile.FileName.Substring(last);
path = Helper.DateAsfileName() + fileExtension; result = savePath + path;
//int start=result.Substring
result = result.Substring(0, result.LastIndexOf("\\")); //上传到服务器后的物理路径(实际路径)
if (!Directory.Exists(result))
{
Directory.CreateDirectory(result);
}
result = path;
fileupload.PostedFile.SaveAs(savePath + result);
}
else
{
result = "文件上传格式不正确!";
return false;
}
}
catch (Exception ex)
{
result = "文件上传错误:\r\n" + ex.Message + " 出错位置:\r\n" + ex.StackTrace;
return false;
}
return true;
} /// <summary>
/// 检测文件上传格式
/// </summary>
/// <param name="hifile"></param>
/// <returns></returns>
public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch { }
r.Close();
fs.Close();//255216是jpg;7173是gif;6677是BMP;13780是PNG;7790是exe,8297是rar
if (fileclass == "255216" || fileclass == "7173" || fileclass == "6677" || fileclass == "13780")
{
return true;
}
else
{
return false;
}
}因为后面还有很多代码,所以就只能用QQ了
{ try
{
int count;
SqlParameter[] parm = new SqlParameter[8];
string picPath = (string)ViewState["Pic"];
if (!string.IsNullOrEmpty(picPath))
{
DeleteFile(Server.MapPath("~/MusicAlbum") + picPath);
}
parm[0] = new SqlParameter("@RecordID", int.Parse(txtRecordID.Text));
string strPicFileName = string.Empty;
if (FileUploadU(MapPath("~/") + @"Res\MusicAlbum\", fuPic,out strPicFileName))
{
parm[1] = new SqlParameter("@Pic", @"\" + strPicFileName);
parm[2] = new SqlParameter("@Title", txtTitle.Text);
parm[3] = new SqlParameter("@SingerName", txtSingerName.Text);
parm[4] = new SqlParameter("@VisitorCount", txtVisitCount.Text);
parm[5] = new SqlParameter("@CreatePeople", txtCreateDate.Text);
parm[6] = new SqlParameter("@EditPeople", txtEditPeople.Text);
parm[7] = new SqlParameter("@EditDate", DateTime.Now); count = RunProcedure("MIndex2NAHotAlbumLarge_Update", parm, out count);
if(count>0)
{
Common.Show(this, "操作成功!");
}
else
{
Common.Show(this, "操作失败!");
}
}
else
{
Common.Show(this, "上传失败,请重新上传!");
}
}
catch (Exception ex)
{
string strErrorMsg = "操作出错,错误信息:" + ex.Message + " 出错位置: " + ex.StackTrace;
Common.Show(this, strErrorMsg.Replace("\r", "\\r").Replace("\n", "\\n"));
}} public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int result;
connection.Open();
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
connection.Close();
return result;
}
}private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command;
} private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
} /// <summary>
/// 输入完整的路径,进行删除文件操作(如果上传同一文件,则执行此方法)
/// </summary>
/// <param name="filePath"></param>
public static void DeleteFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
catch { }
} /// <summary>
/// 上传文件到服务器
/// </summary>
/// <param name="savePath"></param>
/// <param name="fileupload"></param>
/// <param name="result"></param>
/// <returns></returns>
public bool FileUploadU(string savePath, FileUpload fileupload, out string result)
{
//从web.config中读取上传路径 ConfigurationManager.ConnectionStrings["path"].ConnectionString
string path = "";
try
{
//获取文件后缀名
int last = fileupload.PostedFile.FileName.LastIndexOf(".");
//获取指定路径的文件名
string fileExtension = fileupload.PostedFile.FileName.Substring(last + 1);
if (Common.IsAllowedExtension(fileupload) == true)
{
//上传文件后缀名
fileExtension = fileupload.PostedFile.FileName.Substring(last);
path = Helper.DateAsfileName() + fileExtension; result = savePath + path;
//int start=result.Substring
result = result.Substring(0, result.LastIndexOf("\\")); //上传到服务器后的物理路径(实际路径)
if (!Directory.Exists(result))
{
Directory.CreateDirectory(result);
}
result = path;
fileupload.PostedFile.SaveAs(savePath + result);
}
else
{
result = "文件上传格式不正确!";
return false;
}
}
catch (Exception ex)
{
result = "文件上传错误:\r\n" + ex.Message + " 出错位置:\r\n" + ex.StackTrace;
return false;
}
return true;
} /// <summary>
/// 检测文件上传格式
/// </summary>
/// <param name="hifile"></param>
/// <returns></returns>
public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch { }
r.Close();
fs.Close();//255216是jpg;7173是gif;6677是BMP;13780是PNG;7790是exe,8297是rar
if (fileclass == "255216" || fileclass == "7173" || fileclass == "6677" || fileclass == "13780")
{
return true;
}
else
{
return false;
}
}因为后面还有很多代码,所以就只能用QQ了
2013-07-22
展开全部
FileUpload控件。protected void Buttion1_Click(object sender,EventArgs e){ bool filelsValid=false; //如果确认了上传文件,则判断文件类型是否符合要求 if(this.FileUpload.HasFile) { //获取上传文件的后缀 String fileExtension=System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower(); String[] restrictExtension={".gif",".jpg",".bmp",".png"} //判断文件类型是否符合要求 { for(int i=0;i<restrictExtension.Lenght;i++) { if(fileExtension==restrictExtension[i]) { filelsValid=true; } } //如果文件类型符合要求,调用SavaAs方法实现上传,并显示相关信息 if(filelsValid==true) { try { this.Image1.ImageUrl='"~/images/"+FileUpload1.FileName; this.FileUpload.SavaAs(Server.MapPath("~/images/"+FileUpload1.FileName)); Response.Write("<script>alert('文件上传成功!')</script>"); } catch {Response.Write("<script>alert('文件上传失败!')</script>");} } else{Response.Write("<script>alert('只能上传后缀为.gif,.jpg,.bmp,.png的文件!')</script>");} } }}代码是我一个一个敲上来的,可能有些拼写有误,关键在于掌握其方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-22
展开全部
文件上传到某个文件夹就行了,数据库只是保持路径,网上找找很多上传代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询