问题描述
我要做一个上传图片的功能,实现把本地的图片上传到服务器中,那位大大讲解一下,有代码最好
解决方案
解决方案二:
后台可以考虑使用ftp或是http服务器来接受文件,如果采用http的话,给你一个参考server(asp.netmvc)[AcceptVerbs(HttpVerbs.Post)]publicvoidPush(stringid){stringappName=id.Split('.')[0];stringtargetFolder=Path.Combine(Server.MapPath("/Apps"),appName);if(!Directory.Exists(targetFolder)){Directory.CreateDirectory(targetFolder);}varbuffer=newbyte[4096];using(FileStreamfs=newFileStream(Path.Combine(targetFolder,id),FileMode.Create)){while(true){intr=Request.InputStream.Read(buffer,0,4096);if(r<=0){break;}fs.Write(buffer,0,r);}}}
client(csharp)namespaceAppGet{publicclassPushCommand:CommandBase{protectedoverridevoidExecute(){stringuri=string.Format("http://{0}/App/Push/{1}",ConfigurationManager.AppSettings["AppCenter"],Args["-file"]);Console.WriteLine(uri);HttpWebRequestrequest=HttpWebRequest.Create(uri)asHttpWebRequest;request.Method="POST";using(Streamstream=request.GetRequestStream()){varbuffer=newbyte[4096];using(FileStreamfs=newFileStream(Args["-file"],FileMode.Open)){while(true){intr=fs.Read(buffer,0,4096);if(r<=0){break;}stream.Write(buffer,0,r);}}}request.GetResponse();}privateconststringDefaultContentType="application/octet";}}
解决方案三:
我写的代码如下,可是报主机名无效和uri错误怎么解决?publicvoidUpLoad(stringfileName){FileInfofileInfo=newFileInfo(fileName);stringuri="ftp://"+ftpServerIP+"/"+fileInfo.Name;FtpWebRequestreqFTP;reqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(uri));//reqFTP.Credentials=newNetworkCredential(ftpUserID,ftpPassword);reqFTP.KeepAlive=false;reqFTP.Method=WebRequestMethods.Ftp.UploadFile;reqFTP.UseBinary=true;reqFTP.ContentLength=fileInfo.Length;intbuffLength=2048;byte[]buff=newbyte[buffLength];intcontentLen;FileStreamfs=fileInfo.OpenRead();try{Streamstrm=reqFTP.GetRequestStream();contentLen=fs.Read(buff,0,buffLength);while(contentLen!=0){strm.Write(buff,0,contentLen);contentLen=fs.Read(buff,0,buffLength);}strm.Close();fs.Close();}catch(Exceptionex){MessageBox.Show(ex.Message,"UploadError");}}privatevoidbutton1_Click(objectsender,EventArgse){UpLoad(@"E:唐磊练习FtpFtpImage20070616233451.gif");}
解决方案四:
stringuri=@"ftp://"+ftpServerIP+"/"+fileInfo.Name;
解决方案五:
baichangle还是不可以啊还需要在服务器上写什么代码吗?
解决方案六:
nonocast有没有Winfrom用ftp实现图片上传的指教一下撒先谢过了
解决方案七:
System.Net.WebClientwebClient=newSystem.Net.WebClient();参考webClient.UploadFile方法
解决方案八:
引用6楼owennol的回复:
System.Net.WebClientwebClient=newSystem.Net.WebClient();参考webClient.UploadFile方法
+1,这个方法不错。
解决方案九:
6楼的简单。