上传图片文件(支持远程存储)
生活随笔
收集整理的這篇文章主要介紹了
上传图片文件(支持远程存储)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在項目是,需要將圖片保存到另外一臺服務器上,很痛苦!
最后通過FileInfo實現!
/// <summary>/// 保存文件到遠程服務器/// </summary>/// <param name="filepath1">文件路徑,通常通過FileUpLoad.PostedFile.FileName得到</param>public void CopyPic(string filepath1){string filepath = filepath1;if (filepath == string.Empty){//MessageBox.Show("圖片路徑不能為空!");return;}FileInfo info = new FileInfo(filepath);string imgpath = "PCPersonnel/";string dirpath = "//192.168.0.250/U_HomeImage/" + imgpath;try{if (!Directory.Exists(dirpath)){Directory.CreateDirectory(dirpath);}info.CopyTo(dirpath + info.Name, true);//txtSingle.Text = imgpath + info.Name;//MessageBox.Show("圖片上傳成功!"); }catch (Exception ex){//lblShow.Show();//this.lblShow.Text = "圖片上傳失敗,失敗原因:" + ex.Message; }}?
如果保存圖片在本地更簡單了
///fileLoad為FileUpLoad文件 if (this.fileLoad.FileName.Length > 0){if (!Directory.Exists(Server.MapPath("picture/PCPersonnel")))Directory.CreateDirectory(Server.MapPath("picture/PCPersonnel"));//this.DeletePicture();HttpPostedFile hpf = this.fileLoad.PostedFile;string strext = new FileInfo(hpf.FileName).Extension;Random rad = new Random(5);string strpath = string.Format("{0}-{1:X4}-{2:X8}{3}", "" + DateTime.Now.ToString("yyyyMMddHHmmss"), DateTime.Now.Millisecond, rad.GetHashCode(), strext);hpf.SaveAs(Server.MapPath("picture/PCPersonnel/" + strpath));//this.Picture.InnerText = strpath;picPath = "PCPersonnel/" + strpath;}轉載于:https://www.cnblogs.com/PLifeCopyDown/archive/2012/09/26/2704586.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的上传图片文件(支持远程存储)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios12.3的设备管理在哪里(iOS1
- 下一篇: js实现拖拽+碰撞+重力