当前位置:编程学习 > C#/ASP.NET >>

怎么把图片文件上传到指定的文件夹啊

我就知道怎么在文本中写入文字 但不知道怎么把文件放到文件夹  用编程的方式 写在按钮事件下 --------------------编程问答--------------------

File.Copy的方法是:
将现有文件复制到新文件。不允许改写同名的文件。
上面是MSDN定义的解释 
不允许改同名文件的意思是,你复制过去的文件不能有相同的名称的文件。
否则将跳出IOException异常。
这个错误表示:文件已经存在或System.IO异常
这个函数的所带的两个参数,都为绝对路径。
一个为要复制的文件的路径
一个为目标文件的路径
在使用这个方法时候要注意它可能抛出的几个异常。
上面提到的那个异常是其中的一个。  --------------------编程问答-------------------- File.Move("要移动的文件路径", "目标文件路径");要先引用io即using System.IO; --------------------编程问答-------------------- fileupload控件啊 网上全是啊 --------------------编程问答-------------------- c#有专门的控件的啊,搜一下,网上好多例子! --------------------编程问答-------------------- #region 上传项目样品图片
    public void upSamplePhoto_click(object sender, EventArgs e)
    {
        string strFilePath = string.Empty;
        string strSrvFilePath = string.Empty;
        try
        {
            #region 创建目录
            if (!Directory.Exists(Server.MapPath("./SampleUpFile")))
            {
                Directory.CreateDirectory(Server.MapPath("./SampleUpFile"));
            }
            #endregion
            strFilePath = fileUpSample.PostedFile.FileName;
            string strSrvPath = Server.MapPath("./SampleUpFile") + "/";
            string strFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + strFilePath.Substring(strFilePath.LastIndexOf('\\') + 1);
            strSrvFilePath = strSrvPath + strFileName;
            fileUpSample.PostedFile.SaveAs(strSrvFilePath);
        }
        catch (Exception ex)
        {
            clsPublic.scriptAlert(this, "上传图片 " + strFilePath.Substring(strFilePath.LastIndexOf('\\') + 1) + " 失败!");
            return;
        }
        string strSql = "insert into tb_HM_SamplePhoto(TendersNoticeNo,AttachmentUrl) values(@TendersNoticeNo,@AttachmentUrl)";
        pars = new List<SqlParameter>();
        pars.Add(new SqlParameter("@TendersNoticeNo", strNoticeNo));
        pars.Add(new SqlParameter("@AttachmentUrl", strSrvFilePath));
        sqlConnecting.voidExecuteNonQuery(strSql, pars.ToArray());
        clsPublic.scriptAlert(this, "上传图片 " + strFilePath.Substring(strFilePath.LastIndexOf('\\') + 1) + " 成功!");
    }
    #endregion --------------------编程问答-------------------- File.Move
winform 上传图片 openfiledialog
WebClient wc = new WebClient(); 
wc.UploadFile(path, current); 
ftpwebrequest
--------------------编程问答-------------------- web fileupload --------------------编程问答--------------------  string test = Server.MapPath("SwSMenu");  //用来生成文件夹
        if (!Directory.Exists(test))
        {
            Directory.CreateDirectory(test);
        }

 string imgname = fUpLoad.PostedFile.FileName;
                    string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
                    string quanname = Guid.NewGuid() + "." + imgType;
                    string imgurl = "SwSMenu/" + quanname;
                    fUpLoad.PostedFile.SaveAs(Server.MapPath(imgurl));
用fileupload --------------------编程问答--------------------         public bool UpLoad(HtmlInputFile myFile, string num, out string FileName, out string info)
        {
            bool iRen = false;
            info = "";
            string ServerFilePath = string.Empty;
            string str = myFile.PostedFile.FileName;//获取文件路径
            FileInfo myFileInfo = new FileInfo(str);
            string suffix = str.Substring(str.LastIndexOf(".")).ToLower();//后缀  
            string imgType = myFile.PostedFile.ContentType;
            FileName = DateTime.Now.ToString("yyyyMMddhhmmss") + num + suffix;
            double size = myFile.PostedFile.ContentLength;//获取文件大小
            if (size > 1048576)
            {
                info = "文件大小超过一兆";
                iRen = false;
            }
            else
            {
                if (imgType != "image/pjpeg" && imgType != "image/jpg" && imgType != "image/gif" && imgType != "image/bmp" && imgType != "image/png")
                {
                    info = "图片格式不正确";
                    iRen = false;
                }
                else
                {
                    try
                    {
                        ServerFilePath = Server.MapPath("../UpLoadFiles/SelfSitePic") + "\\" + FileName; //绑定到指定的路径
                        myFileInfo = new FileInfo(ServerFilePath);
                        if (myFileInfo.Exists)
                        {
                            myFileInfo.Delete();
                        }
                        myFile.PostedFile.SaveAs(ServerFilePath);
                        iRen = true;
                    }
                    catch (Exception myEx)
                    {
                        throw myEx;
                    }
                }
            }
            return iRen;
        }  这个是我写的一个方法 可以用  --------------------编程问答-------------------- 界面上有一个Fileupload控件id="fileup" 如果存放在images中则
string savePath="images";
savePath=server.MapPath(savePath);
fileup.saveAs(savePath); --------------------编程问答-------------------- Server.MapPath 看不明白 谁能举个简单的例子 --------------------编程问答-------------------- 用法:
1.Server.MapPath("/")  应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\
2.Server.MapPath("./")  表示所在页面的当前目录 
    注:等价于Server.MapPath("")  返回 Server.MapPath("")所在页面的物理文件路径
3.Server.MapPath("../")表示上一级目录 
4.Server.MapPath("~/")表示当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置 如:C:\Inetpub\wwwroot\Example\
注:等效于Server.MapPath("~")。

--------------------编程问答-------------------- http://wenku.baidu.com/view/5f08895f804d2b160b4ec025.html --------------------编程问答-------------------- fileupload --------------------编程问答-------------------- 楼主  你要的是web  还是  winForm的

如果是Web   用FileUpLoad控件  然后调用SaveAs()方法

如果是WinForm  若只是本地直接Copy就行。如需要上传,需要用Socket了 --------------------编程问答-------------------- 图片上传的路径是可以指定的嘛

你要弄个非法的路径 你看你传得成功不!!! --------------------编程问答--------------------
引用 9 楼 hikor 的回复:
public bool UpLoad(HtmlInputFile myFile, string num, out string FileName, out string info)
  {
  bool iRen = false;
  info = "";
  string ServerFilePath = string.Empty;
  string str = myFile.Post……



恩~~~楼主试试哈~~~~~ --------------------编程问答-------------------- 是不是还需要调用什么类呀?我粘上去以后,报错说有好几个没有定义的变量! --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,