上传得时候要是我上传得文件名是中文的话,会出现乱码阿,有什么办法解决吗
下载的时候怎样去判断服务器上有没有这个文件,有的话才下载,没有的话弹出提示(我是将文件路径存入数据库的,所以有可能发生服务器上的文件被删除,但是数据库中的对应的内容没有删掉)

解决方案 »

  1.   

    1.web.config中把字符集设置为gb2312
    2.你的下载文件是使用一个程序来控制的吗?还是直接链接文件的路径?
    如果是第一种的话,你可以用System.IO.File.Exists()来判断服务器端是否存在该文件如果是第二种的话,可以用HttpRequest来直接访问文件的url地址,如http://www.mysite.com/a.doc如果有响应的话,表示该文件存在
      

  2.   

    1.web.config中把字符集设置为gb2312
      

  3.   

    文件上传
    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();
    }
      

  4.   

    打开web.config
     requestEncoding="utf-8" 
     responseEncoding="utf-8" 
    改为
    requestEncoding="gb2312" 
    responseEncoding="gb2312"
      

  5.   

    先取出包含这个文件路径的字段,然后
    dim file1 as system.io.file
    if file1.exists(文件路径) then
    //这里写处理的代码!
    end if