关于上传与下载的问题 上传得时候要是我上传得文件名是中文的话,会出现乱码阿,有什么办法解决吗下载的时候怎样去判断服务器上有没有这个文件,有的话才下载,没有的话弹出提示(我是将文件路径存入数据库的,所以有可能发生服务器上的文件被删除,但是数据库中的对应的内容没有删掉) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.web.config中把字符集设置为gb23122.你的下载文件是使用一个程序来控制的吗?还是直接链接文件的路径?如果是第一种的话,你可以用System.IO.File.Exists()来判断服务器端是否存在该文件如果是第二种的话,可以用HttpRequest来直接访问文件的url地址,如http://www.mysite.com/a.doc如果有响应的话,表示该文件存在 1.web.config中把字符集设置为gb2312 文件上传private void Button1_Click(object sender, System.EventArgs e){ //定义文件流 string imgName_value; string imgContentType; string imgUploadedName; //FileInfo file =new FileInfo(UploadFile.Value); Stream imgStream = UploadFile.PostedFile.InputStream; //选取文件 int imgLen = UploadFile.PostedFile.ContentLength; //获取文件长度 imgUploadedName = UploadFile.PostedFile.FileName; //获取文件名 byte[] imgBinaryData=new byte[imgLen]; imgContentType = UploadFile.PostedFile.ContentType; //获取文件类型 imgName_value = imgName.Value; //取文本框的文件名 try { if(imgName_value.Length < 1) { imgName_value = GetLastRightOf("\\",imgUploadedName ); //GetLastRightOf()函数,是返回指定字符最右边的字符串 imgName.Value = imgName_value; } } catch(Exception myEx) { Response.Write(myEx.Message); } int n = imgStream.Read(imgBinaryData, 0, imgLen); //从当前流读取字节 int NumRowsAffected = MyDatabaseMethod(imgName_value, imgBinaryData, imgContentType, imgLen); //MyDatabaseMethod函数,第一个参数是保存文件名,第二个参数是保存文件流,第三个参数是保存文件类型 if(NumRowsAffected > 0) Response.Write( "<BR> uploaded image " ); else Response.Write ( "<BR> an error occurred uploading the image.d " ); } public string GetLastRightOf(string LookFor,string myString) { int StrPos; StrPos = myString.LastIndexOf(LookFor); //返回字符串最后一个匹配项的位置 return myString.Substring(StrPos + 1); //根据上面找到的位置截取字符串 } public int MyDatabaseMethod(string imgName,byte[] imgbin,string imgcontenttype,int imglen) { SqlConnection connection = new SqlConnection("server=zm;uid=sa;database=pubs"); string SQL="INSERT INTO ImageStore (IMGTitle,IMGData,IMGType,IMGLen) VALUES ( @img_name,@img_data,@img_type,@img_len)"; SqlCommand command=new SqlCommand ( SQL,connection ); command.Parameters.Add(new SqlParameter("@img_name", SqlDbType.VarChar,50)); command.Parameters["@img_name"].Value = imgName; command.Parameters.Add(new SqlParameter("@img_data", SqlDbType.Image)); command.Parameters["@img_data"].Value = imgbin; command.Parameters.Add(new SqlParameter("@img_type", SqlDbType.VarChar,50)); command.Parameters["@img_type"].Value = imgcontenttype; command.Parameters.Add(new SqlParameter("@img_len", SqlDbType.BigInt,8)); command.Parameters["@img_len"].Value = imglen; connection.Open(); int numRowsAffected = command.ExecuteNonQuery(); //上传成功,返回插入行数 connection.Close(); return numRowsAffected;}文件下载部分private void Button2_Click(object sender, System.EventArgs e){ string imgid =imgName.Value; string filename; string sql="SELECT imgtitle,imgdata,imgtype FROM ImageStore WHERE id = " + imgid; SqlConnection connection = new SqlConnection("server=zm;uid=sa;database=pubs"); SqlCommand command = new SqlCommand(sql, connection); connection.Open(); SqlDataReader dr = command.ExecuteReader(); if(dr.Read()) { filename="attachment;filename="+HttpUtility.UrlEncode(dr["imgtitle"].ToString(),System.Text.Encoding.UTF8); Response.AppendHeader("Content-Disposition",filename); Response.BinaryWrite( (byte[]) dr["imgdata"] ); } connection.Close();} 打开web.config requestEncoding="utf-8" responseEncoding="utf-8" 改为requestEncoding="gb2312" responseEncoding="gb2312" 先取出包含这个文件路径的字段,然后dim file1 as system.io.fileif file1.exists(文件路径) then//这里写处理的代码!end if 安装Silverlight 老是出这样的错误。。求解决方案 那个高手能帮我把这段asp代码转换成.net ? 谁有简单的3层架构源码 【求电子书籍,在线等,谢谢大家】 Panel动态添加控件后的位置问题 asp.net ajax控件与母版的问题 sql语句:case...when 小菜:求一用户添加后台代码 登陆验证之问题 送分......100 如何设置 vs2010 停止调试时不关闭浏览器 求助!如何将含有dropdownlist控件(非编辑状态)的datagrid里的数据导出到excel或word里? 自己写的小程序,中文繁简通,欢迎下载。散分!!!
2.你的下载文件是使用一个程序来控制的吗?还是直接链接文件的路径?
如果是第一种的话,你可以用System.IO.File.Exists()来判断服务器端是否存在该文件如果是第二种的话,可以用HttpRequest来直接访问文件的url地址,如http://www.mysite.com/a.doc如果有响应的话,表示该文件存在
private void Button1_Click(object sender, System.EventArgs e)
{
//定义文件流
string imgName_value;
string imgContentType;
string imgUploadedName; //FileInfo file =new FileInfo(UploadFile.Value);
Stream imgStream = UploadFile.PostedFile.InputStream; //选取文件
int imgLen = UploadFile.PostedFile.ContentLength; //获取文件长度
imgUploadedName = UploadFile.PostedFile.FileName; //获取文件名
byte[] imgBinaryData=new byte[imgLen];
imgContentType = UploadFile.PostedFile.ContentType; //获取文件类型
imgName_value = imgName.Value; //取文本框的文件名
try
{
if(imgName_value.Length < 1)
{
imgName_value = GetLastRightOf("\\",imgUploadedName ); //GetLastRightOf()函数,是返回指定字符最右边的字符串
imgName.Value = imgName_value;
}
}
catch(Exception myEx)
{
Response.Write(myEx.Message);
} int n = imgStream.Read(imgBinaryData, 0, imgLen); //从当前流读取字节
int NumRowsAffected = MyDatabaseMethod(imgName_value, imgBinaryData, imgContentType, imgLen); //MyDatabaseMethod函数,第一个参数是保存文件名,第二个参数是保存文件流,第三个参数是保存文件类型
if(NumRowsAffected > 0)
Response.Write( "<BR> uploaded image " );
else
Response.Write ( "<BR> an error occurred uploading the image.d " ); }
public string GetLastRightOf(string LookFor,string myString)
{
int StrPos;
StrPos = myString.LastIndexOf(LookFor); //返回字符串最后一个匹配项的位置
return myString.Substring(StrPos + 1); //根据上面找到的位置截取字符串
} public int MyDatabaseMethod(string imgName,byte[] imgbin,string imgcontenttype,int imglen)
{
SqlConnection connection = new SqlConnection("server=zm;uid=sa;database=pubs");
string SQL="INSERT INTO ImageStore (IMGTitle,IMGData,IMGType,IMGLen) VALUES ( @img_name,@img_data,@img_type,@img_len)";
SqlCommand command=new SqlCommand ( SQL,connection ); command.Parameters.Add(new SqlParameter("@img_name", SqlDbType.VarChar,50));
command.Parameters["@img_name"].Value = imgName;
command.Parameters.Add(new SqlParameter("@img_data", SqlDbType.Image));
command.Parameters["@img_data"].Value = imgbin; command.Parameters.Add(new SqlParameter("@img_type", SqlDbType.VarChar,50));
command.Parameters["@img_type"].Value = imgcontenttype; command.Parameters.Add(new SqlParameter("@img_len", SqlDbType.BigInt,8));
command.Parameters["@img_len"].Value = imglen;
connection.Open();
int numRowsAffected = command.ExecuteNonQuery(); //上传成功,返回插入行数
connection.Close();
return numRowsAffected;
}文件下载部分
private void Button2_Click(object sender, System.EventArgs e)
{
string imgid =imgName.Value;
string filename;
string sql="SELECT imgtitle,imgdata,imgtype FROM ImageStore WHERE id = " + imgid;
SqlConnection connection = new SqlConnection("server=zm;uid=sa;database=pubs");
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
filename="attachment;filename="+HttpUtility.UrlEncode(dr["imgtitle"].ToString(),System.Text.Encoding.UTF8);
Response.AppendHeader("Content-Disposition",filename);
Response.BinaryWrite( (byte[]) dr["imgdata"] );
}
connection.Close();
}
requestEncoding="utf-8"
responseEncoding="utf-8"
改为
requestEncoding="gb2312"
responseEncoding="gb2312"
dim file1 as system.io.file
if file1.exists(文件路径) then
//这里写处理的代码!
end if