.Net使用FTP模式跨域上传图片

using System;
using System.IO;
using System.Net;
using System.Web.UI.WebControls;

namespace ZC.Utils
{
    public class FTPHelper
    {
        public string Upload(FileUpload fileUpload, string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            string filename = fileUpload.FileName;
            string sRet = "上传成功!";
            FileInfo fileInf = new FileInfo(fileUpload.PostedFile.FileName);
            string uri = "ftp://" + ftpServerIP + "/" + filename;
            FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.KeepAlive = false;
            reqFTP.Method = "STOR";
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            FileStream fs = fileInf.OpenRead();
            try
            {
                Stream strm = reqFTP.GetRequestStream();
                for (int contentLen = fs.Read(buff, 0, buffLength); contentLen != 0; contentLen = fs.Read(buff, 0, buffLength))
                {
                    strm.Write(buff, 0, contentLen);
                }
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
            return sRet;
        }

public string Download(string userId, string pwd, string ftpPath, string filePath, string fileName)
        {
            string sRet = "下载成功!";
            try
            {
                FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
                FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath + fileName));
                reqFTP.Method = "RETR";
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;
                reqFTP.Credentials = new NetworkCredential(userId, pwd);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                byte[] buffer = new byte[bufferSize];
                for (int readCount = ftpStream.Read(buffer, 0, bufferSize); readCount > 0; readCount = ftpStream.Read(buffer, 0, bufferSize))
                {
                    outputStream.Write(buffer, 0, readCount);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
            return sRet;
        }

public string DeleteFile(string ftpPath, string userId, string pwd, string fileName)
        {
            string sRet = "删除成功!";
            FtpWebResponse Respose = null;
            Stream localfile = null;
            Stream stream = null;
            try
            {
                FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}{1}", ftpPath, fileName)));
                reqFTP.Credentials = new NetworkCredential(userId, pwd);
                reqFTP.KeepAlive = false;
                reqFTP.Method = "DELE";
                Respose = (FtpWebResponse)reqFTP.GetResponse();
            }
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
            finally
            {
                if (Respose != null)
                {
                    Respose.Close();
                }
                if (localfile != null)
                {
                    localfile.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return sRet;
        }
    }
}

时间: 2024-08-29 17:46:45

.Net使用FTP模式跨域上传图片的相关文章

跨域上传图片的尝试过程,最终成功了。哈哈

关于这个跨域上传图片的问题,其实去年底的时候就该去实现的,因为老板朝三暮四,一会儿让做这个,一会儿看那个,就耽误了.因为这个过程花费了我整整一天的时间,我认为有必要记录下来. 首先,项目是一个ERP,针对的是公司的一个小型电商的网站.项目经理让我搭建一个图片服务器, 当时也是为了完(尝)成(尝)任(新)务(鲜),用的都是当时从没接触过的.通过百度就选择了 Nginx + Nodejs + express + ImageMagick 来实现的. 当时还简单看了一下淘宝的TFS(Taobao Fil

让 umeditor 支持跨域上传图片

事实上,umeditor 上传图片采用的是 form+隐藏iframe方式,已经能做到无刷新跨域上传.但小小遗憾的是做的并不完美,上传图片后,反馈的json信息,js并不能读取.原因在于,js不能操作跨域的iframe. 1 var r = this.contentWindow.document.body.innerHTML; 此句代码在FF Chrome上都会报错.浏览器的这种限制,可以通过两个方式来解决 换成flash上传 主页跟iframe页都设置 document.domain . 第一

ueditor富文本编辑器跨域上传图片解决办法

在使用百度富文本编辑器的过程中,如果是有一台单独的图片服务器就需要将上传的图片内容放到图片服务器,也就是比如在a.com的编辑器上传图片,图片要保存到img.com的跨域上传图片功能,而在ueditor官方文档中说不支持单图上传的跨域, 网上查了一下各种花里胡哨,一顿操作猛如虎,比如加document.domain,配置全域名的等等都是然并卵,我仔细研究了一下ueditor的demo文件,相出了一个折中办法,很简单只需要修改demo中两个地方的代码外加写一个上传接口即可. 首先引入页面uedit

kindeditor4跨域上传图片解决

项目中正在使用kindeditor, 版本号4.1.10 非常多公司的图片会走CDN,须要单独的一台图片上传服务如:(upload.268xue.com) kindeditor上传图片的简单内部流程: 上传button是提交到iframe.生成1个form和1个iframe,form提交到(arget)iframe iframe的onload方法获取返回的值.然后调用配置回调方法afterUpload/afterError. 详细实现:(在a.com选中图片,上传用upload.268xue.c

·基于thinkphp5.0和百度编辑器UMeditor 跨域上传图片实现

1.下载两者源码: 2,简单配置 :   a. 一套thinkphp框架是显示前端界面,搭配编辑器环境.对应域名为 http://tp.com:  这是前端界面配置 但是还要修改一下umeditor.config.js 文件    这里配置那个URL ,我思考了很久         我是这么想的  本地站点域名配置到了www/tp5/public位置 ,现在是要实例编辑器加一个路径,而编辑器的文件位置就在public下面: 在此情况下 编辑器会在界面正常显示: 好接下来考虑的问题就是图片上传了.

iframe跨域上传图片

方案一:用jquery的$.post异步提交,然后把返回来的值用jquery填充到隐藏域中.可是$.post不支持跨域. jQuery.ajax()支持get方式的跨域,这其实是采用jsonp的方式来完成的. 方案二:利用iframe以及jquery进行表单post提交.代码如下: == a.com/post.html == <script> function postcallback(data){ //code... } </script> <form action=&qu

php 之跨域上传图片

因为要将所有上传的图片上传到一台独立的图片服务器上面,js上传时存在跨域问题,网上找到这种,通过php curl方式,将图片重新发送到另外一台服务器上保存,并返回图片路径! 这种方式存在一定问题:1,上传大图片时,比如2M,需要将图片首先传到服务器,然后再传到图片服务器,中间需要4M的传输,加大了上传时间!2,从服务器转到图片服务器的时候因为没有保存,传输使用的是临时文件,考虑到图片格式的限制,将图片进行了重命名,传输之后对该图片进行删除,碰到一些未知问题是可能导致图片删除失败,增加了服务器存储

WebClient实现kindeditor跨域上传图片

尝试上传图片到一个单独的服务器,使用的是KindeEditor 4.1.10 kindeditor上传图片原理是: 上传按钮提交到iframe,生成1个form和1个iframe,form提交到(arget)iframeiframe的onload方法获取返回的值,然后调用配置回调方法afterUpload/afterError. 第一次尝试 页面A document.domain = "aaa.xx.com"; 图片服务器B document.domain = "bbb.xx

Kindeditor跨域上传图片在ASP.NET方案中的解决

虽然标题写的是ASP.NET的解决方案,但是也可用在PHP,Jsp中.直接上代码 首先在客户端中配置 var editor; document.domain = window.location.host;//js解析域 KindEditor.ready(function (k) { editor = k.create('#editor_id', { uploadJson: GlobalConfig .RestServiceUrl + "/FileUpload/upload_json.ashx?f