Uploading files is a relatively common function, the previous period of time to do a upload image module. Started with the way you shared folders, and later found this method not very good. So decisively kill it, and then choose to use the FTP way to upload. Personally feel the FTP way is still more useful, so share with you.
/// <summary> ///ftp upload files/// </summary> /// <param name= "FileUpload" >Uploading Controls</param> /// <param name= "Ftpserverip" >upload file server IP</param> /// <param name= "Ftpuserid" >Server user name</param> /// <param name= "FTPPassword" >Server Password</param> /// <returns></returns> Public stringUpload (FileUpload FileUpload,stringFtpserverip,stringFtpuserid,stringFTPPassword) { stringfilename =Fileupload.filename; stringSret ="Upload Success! "; FileInfo Fileinf=NewFileInfo (fileUpload.PostedFile.FileName); stringURI ="ftp://"+ Ftpserverip +"/"+filename; FtpWebRequest reqftp; //to create a FtpWebRequest object from a URIReqftp = (ftpwebrequest) ftpwebrequest.create (NewUri (URI)); //FTP user name and passwordReqftp.credentials =NewNetworkCredential (Ftpuserid, FTPPassword); //The default is true and the connection is not closed//is executed after a commandReqftp.keepalive =false; //specify what commands to executeReqftp.method =WebRequestMethods.Ftp.UploadFile; //specifying data Transfer TypesReqftp.usebinary =true; Reqftp.usepassive=false; //notifies server file size when uploading a fileReqftp.contentlength =fileinf.length; //buffer size set to 2KB intBuffLength =2048; byte[] Buff =New byte[BuffLength]; intContentlen; //open a file stream (System.IO.FileStream) to read the uploaded fileFileStream fs =Fileinf.openread (); Try { //write the uploaded file to the streamStream STRM =Reqftp.getrequeststream (); //2kb per read file streamContentlen = fs. Read (Buff,0, bufflength); //stream content does not end while(Contentlen! =0) { //write content from file stream to upload streamStrm. Write (Buff,0, Contentlen); Contentlen= fs. Read (Buff,0, bufflength); } //Close two streamsSTRM. Close (); Fs. Close (); } Catch(Exception ex) {Sret=Ex. Message; } returnSret; }
The above is just a simple upload, of course, before uploading also need to do some validation of the file, such as file format or file size. For simple verification, please refer to the upload image before uploading to determine the file format and size.
To prevent duplicate names, you can use the global unique identifier GUID to generate a random sequence that, ideally, does not generate two identical GUIDs for any computer or computer cluster. Of course, the odds of repetition are not 0, but they are very small.
/// <summary> /// generate globally unique identifiers /// </summary> /// <returns></returns> Public string strGUID () { string strguid = guid.newguid (). ToString (); return strGUID;}
Calling this method and then stitching the returned sequence with the file name can effectively avoid the case of duplicate file names. Of course, you can use the system's current time to join the file name, so maybe you feel more insured. Specific use of that method is a matter of opinion.
FTP file operation upload file