2018年01月10日 22:25
原创作品,转载时请务必以超链接形式标明文章原始出处,否则将追究法律责任。

天太冷,废话不多说,直接上图

082103934.png

点击Submit按钮直接生成一个Word,并上传至FTP服务器。靠,我本机没有IIS也没有FTP,怎么办,百度google,自己安装一下。我的机子是Win7。进入控制面板,选择程序和功能

161214933.png

点击打开或者关闭Windows功能,弹出如下界面

161448424.png

按照上面的勾选。安装可能需要几分钟的时间,安装完成之后,我们就可以使用IIS和FTP了。在控制面板,管理工具中,打开IIS管理器

162123837.png

IIS管理器打开以后如下

162419882.png

然后浏览默认网站,选中Default WebSite,在组右边栏目里点击浏览*:80(http)

162454467.png

出现如下界面

162647717.png

说明我们的IIS安装成功了。OK,在这里代码我就不展示了,大家在附件中看

163041267.png

接下来我们来部署一下,首先是publish,一步一步往下走,在web程序集上点击右键选择布。

164429768.png

这里不用管,直接下一步

164455281.png

在这里选择文件系统,目标位置选择到你计算机上的某个文件夹,下一步。

164550553.png

选择Release模式,勾选发布前删除所有文件,下一步

164649417.png

点击发布按钮,开始发布

164716207.png

发布成功,我们去刚才设置的发布目录下看看有没有发布过去

164858578.png

没问题,文件已经发布好了。接下来要在IIS创建站点,在网站上点击右键,选择添加网站

165359247.png

然后注意物理路径(发布的路径)和端口就好了。然后配置应用程序池,将MyWebApp应用程序池的.net版本设置为4.0。

165719462.png

OK,我们的web站点搭建好了,我么你还需要搭建FTP服务器

在这里没什么好解释的,在网站上点击右键然后选择添加FTP站点,分以下三步走

181041682.png

181106474.png

181123583.png

点击完成之后,FTP站点就搭建好了,如下

082623532.png

接下里我们用程序生成一个Word,看能否上传至FTP服务器。

182230556.png

提交成功了,我们去在浏览器输入ftp://127.0.0.1:21

181402429.png

点击链接,打开word,我们看到下图已经直接在IE中打开了

181633553.png


最后我们看一下代码

private void BulidWordDocumentAndUploadFTP()
        {
            Object nothing = System.Reflection.Missing.Value;
            string directory = AppDomain.CurrentDomain.BaseDirectory + "Word";
            Directory.CreateDirectory(directory);
            object fileName = Path.Combine(directory, string.Concat(Environment.UserName+Guid.NewGuid().ToString(), ".doc"));
            _Application WordApp = new Application();
            Document WordDoc = WordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
            /*加入段落1 */
            WordApp.Selection.ParagraphFormat.LineSpacing = 11f;//设置段落的距离为11f
            Paragraph para1 = WordDoc.Paragraphs.Add(ref nothing);//对para1进行初始化
            para1.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            para1.Range.Text = "RESPONSE TO LLD3 ::: ABSTRACT SUBMISSION";//对段落para1进行添加内容
            para1.Format.CharacterUnitFirstLineIndent = 0;//设置首行缩进
            para1.Range.Font.Color = Word.WdColor.wdColorBlack;//设置字体的颜色
            para1.Range.Font.Bold = 2;
            para1.Range.Font.Size = 16;
            para1.Range.InsertParagraphAfter();//将段落para1添加到文档中
            object WdLine = WdUnits.wdLine;//换一行;
            object count = 5;
            WordApp.Selection.MoveDown(ref WdLine, ref count, ref nothing);//移动焦点
            WordApp.Selection.TypeParagraph();//插入段落
            Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, int.Parse(this.hfdRowCount.Value) - 2, 6, ref nothing, ref nothing);
            newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
            newTable.Range.Font.Bold = 0;
            newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            newTable.Rows[1].Range.Shading.ForegroundPatternColor = WdColor.wdColorBlueGray;
            newTable.Rows[1].Range.Font.Size = 14;
            newTable.Rows[1].Range.Font.Bold = 2;
            newTable.Rows[1].Range.Font.Color = WdColor.wdColorWhite;
            newTable.Rows[4].Range.Shading.ForegroundPatternColor = WdColor.wdColorBlueGray;
            newTable.Rows[4].Range.Font.Size = 14;
            newTable.Rows[4].Range.Font.Bold = 2;
            newTable.Rows[4].Range.Font.Color = WdColor.wdColorWhite;
            newTable.Rows[6].Range.Shading.ForegroundPatternColor = WdColor.wdColorBlueGray;
            newTable.Rows[6].Range.Font.Size = 14;
            newTable.Rows[6].Range.Font.Bold = 2;
            newTable.Rows[6].Range.Font.Color = WdColor.wdColorWhite;
            newTable.Rows[10].Range.Shading.ForegroundPatternColor = WdColor.wdColorBlueGray;
            newTable.Rows[10].Range.Font.Size = 14;
            newTable.Rows[10].Range.Font.Bold = 2;
            newTable.Rows[10].Range.Font.Color = WdColor.wdColorWhite;
            newTable.Rows[13].Range.Shading.ForegroundPatternColor = WdColor.wdColorBlueGray;
            newTable.Rows[13].Range.Font.Size = 14;
            newTable.Rows[13].Range.Font.Bold = 2;
            newTable.Rows[13].Range.Font.Color = WdColor.wdColorWhite;
            newTable.Rows[2].Range.Font.Size = 12;
            newTable.Rows[3].Range.Font.Size = 12;
            newTable.Rows[5].Range.Font.Size = 12;
            newTable.Rows[7].Range.Font.Size = 12;
            newTable.Rows[8].Range.Font.Size = 12;
            newTable.Rows[9].Range.Font.Size = 12;
            newTable.Rows[11].Range.Font.Size = 12;
            newTable.Rows[12].Range.Font.Size = 12;
            newTable.Cell(1, 1).Range.Text = "PersonalInformation";
            newTable.Cell(1, 1).Range.Font.Color = WdColor.wdColorWhite;
            newTable.Cell(1, 1).Merge(newTable.Cell(1, 6));
            newTable.Cell(2, 1).Range.Text = "Title";
            newTable.Cell(2, 2).Range.Text = txtTitle.Text.Trim();
            newTable.Cell(2, 3).Range.Text = "First Name";
            newTable.Cell(2, 4).Range.Text = txtFirstName.Text.Trim();
            newTable.Cell(2, 5).Range.Text = "Last Name";
            newTable.Cell(2, 6).Range.Text = txtLastName.Text.Trim();
            newTable.Cell(3, 1).Range.Text = "Affiliation";
            newTable.Cell(3, 2).Range.Text = txtAffilication.Text.Trim();
            newTable.Cell(3, 2).Merge(newTable.Cell(3, 6));
            newTable.Cell(4, 1).Range.Text = "Contact Details";
            newTable.Cell(4, 1).Range.Font.Color = WdColor.wdColorWhite;
            newTable.Cell(4, 1).Merge(newTable.Cell(4, 6));
            newTable.Cell(5, 1).Range.Text = "Email address";
            newTable.Cell(5, 2).Range.Text = txtEmail.Text.Trim();
            newTable.Cell(5, 2).Merge(newTable.Cell(5, 3));
            newTable.Cell(5, 3).Range.Text = "Phone number";
            newTable.Cell(5, 4).Range.Text = txtPhoneNumber.Text.Trim();
            newTable.Cell(5, 4).Merge(newTable.Cell(5, 5));
            newTable.Cell(6, 1).Range.Text = "Address";
            newTable.Cell(6, 2).Merge(newTable.Cell(6, 6));
            newTable.Cell(7, 1).Range.Text = "Street Address";
            newTable.Cell(7, 2).Range.Text = txtStreetAdress.Text.Trim();
            newTable.Cell(7, 2).Merge(newTable.Cell(7, 6));
            newTable.Cell(8, 1).Range.Text = "City";
            newTable.Cell(8, 2).Range.Text = txtCity.Text.Trim();
            newTable.Cell(8, 2).Merge(newTable.Cell(8, 3));
            newTable.Cell(8, 3).Range.Text = "State/Province/Religion";
            newTable.Cell(8, 4).Range.Text = txtReligion.Text.Trim();
            newTable.Cell(8, 4).Merge(newTable.Cell(8, 5));
            newTable.Cell(9, 1).Range.Text = "Country";
            newTable.Cell(9, 2).Range.Text = txtCountry.Text.Trim();
            newTable.Cell(9, 2).Merge(newTable.Cell(9, 3));
            newTable.Cell(9, 3).Range.Text = "Zip/postal Code";
            newTable.Cell(9, 4).Range.Text = txtPostCode.Text.Trim();
            newTable.Cell(9, 4).Merge(newTable.Cell(9, 5));
            newTable.Cell(10, 1).Range.Text = "Abstract Information";
            newTable.Cell(10, 1).Merge(newTable.Cell(10, 6));
            newTable.Cell(11, 1).Range.Text = "Abstract title";
            newTable.Cell(11, 2).Range.Text = txtAbstractTitle.Text.Trim();
            newTable.Cell(11, 1).Range.Text = "Abstract title";
            newTable.Cell(11, 2).Range.Text = txtAbstractTitle.Text.Trim();
            newTable.Cell(11, 2).Merge(newTable.Cell(11, 5));
            newTable.Cell(12, 1).Range.Text = "Presenter(s)";
            newTable.Cell(12, 2).Range.Text = txtPresenter.Text.Trim();
            newTable.Cell(12, 2).Merge(newTable.Cell(12, 3));
            newTable.Cell(12, 3).Range.Text = "Presentation Type";
            newTable.Cell(12, 4).Range.Text = txtPresentationType.Text.Trim();
            newTable.Cell(12, 4).Merge(newTable.Cell(12, 5));
            newTable.Cell(13, 1).Range.Text = "Abstract";
            newTable.Cell(13, 1).Merge(newTable.Cell(13, 6));
            newTable.Cell(14, 1).Range.Text = txtContent.Value.Trim();
            newTable.Cell(14, 1).Merge(newTable.Cell(14, 6));
            newTable.Cell(14, 1).Height = 200;
            newTable.Cell(14, 1).Range.Font.Size = 12;
            newTable.Cell(14, 1).Range.Font.Name = "Times New Roman";
            newTable.Cell(14, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            newTable.Cell(14, 1).Range.ParagraphFormat.SpaceAfter = 1;
            WordDoc.SaveAs(ref fileName, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
            WordDoc.Close(ref nothing, ref nothing, ref nothing);
            FtpHelper ftpHelper = new FtpHelper("127.0.0.1:21", string.Empty, null, null);
            ftpHelper.Upload(fileName.ToString());
        }
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
namespace SjtuEdu
{
    public class FtpHelper
    {
        string ftpServerIP;
        string ftpRemotePath;
        string ftpUserID;
        string ftpPassword;
        string ftpURI;
        /// <summary>
        /// 连接FTP
        /// </summary>
        /// <param name="FtpServerIP">FTP连接地址</param>
        /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
        /// <param name="FtpUserID">用户名</param>
        /// <param name="FtpPassword">密码</param>
        public FtpHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
        {
            ftpServerIP = FtpServerIP;
            ftpRemotePath = FtpRemotePath;
            ftpUserID = FtpUserID;
            ftpPassword = FtpPassword;
            ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
        }
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="filename"></param>
        public void Upload(string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = ftpURI + fileInf.Name;
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            FileStream fs = fileInf.OpenRead();
            try
            {
                Stream strm = 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 (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "Upload Error --> " + ex.Message);
            }
        }
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        public void Download(string filePath, string fileName)
        {
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
 int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --> " + ex.Message);
            }
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName"></param>
        public void Delete(string fileName)
        {
            try
            {
                string uri = ftpURI + fileName;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
                string result = String.Empty;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                long size = response.ContentLength;
                Stream datastream = response.GetResponseStream();
                StreamReader sr = new StreamReader(datastream);
                result = sr.ReadToEnd();
                sr.Close();
                datastream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "Delete Error --> " + ex.Message + "  文件名:" + fileName);
            }
        }
        /// <summary>
        /// 获取当前目录下明细(包含文件和文件夹)
        /// </summary>
        /// <returns></returns>
        public string[] GetFilesDetailList()
        {
            string[] downloadFiles;
            try
            {
                StringBuilder result = new StringBuilder();
                FtpWebRequest ftp;
                ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
                ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                WebResponse response = ftp.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                line = reader.ReadLine();
                line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    result.Remove(result.ToString().LastIndexOf("\n"), 1);
                    reader.Close();
                    response.Close();
                }
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                downloadFiles = null;
                Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFilesDetailList Error --> " + ex.Message);
                return downloadFiles;
            }
        }
        /// <summary>
        /// 获取当前目录下文件列表(仅文件)
        /// </summary>
        /// <returns></returns>
        public string[] GetFileList(string mask)
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                while (line != null)
                {
                    if (mask.Trim() != string.Empty && mask.Trim() != "*.*")
                    {
                        string mask_ = mask.Substring(0, mask.IndexOf("*"));
                        if (line.Substring(0, mask_.Length) == mask_)
                        {
                            result.Append(line);
                            result.Append("\n");
                        }
                    }
                    else
                    {
                        result.Append(line);
                        result.Append("\n");
                    }
                    line = reader.ReadLine();
                }
                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();
                return result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                downloadFiles = null;
                if (ex.Message.Trim() != "远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)。")
                {
                    Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileList Error --> " + ex.Message.ToString());
                }
                return downloadFiles;
            }
        }
        /// <summary>
        /// 获取当前目录下所有的文件夹列表(仅文件夹)
        /// </summary>
       /// <returns></returns>
        public string[] GetDirectoryList()
        {
            string[] drectory = GetFilesDetailList();
            string m = string.Empty;
            foreach (string str in drectory)
            {
                if (str.Trim().Substring(0, 1).ToUpper() == "D")
                {
                    m += str.Substring(54).Trim() + "\n";
                }
            }
            char[] n = new char[]{'\n'};
            return m.Split(n);
        }
       
        /// <summary>
        /// 判断当前目录下指定的子目录是否存在
        /// </summary>
        /// <param name="RemoteDirectoryName">指定的目录名</param>
        public bool DirectoryExist(string RemoteDirectoryName)
        {
            string[] dirList = GetDirectoryList();
            foreach (string str in dirList)
            {
                if (str.Trim() == RemoteDirectoryName.Trim())
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 判断当前目录下指定的文件是否存在
        /// </summary>
        /// <param name="RemoteFileName">远程文件名</param>
        public bool FileExist(string RemoteFileName)
        {
            string[] fileList = GetFileList("*.*");
            foreach (string str in fileList)
            {
                if (str.Trim() == RemoteFileName.Trim())
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="dirName"></param>
        public void MakeDir(string dirName)
        {
            FtpWebRequest reqFTP;
            try
            {
                // dirName = name of the directory to create.
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "MakeDir Error --> " + ex.Message);
            }
        }
        /// <summary>
        /// 获取指定文件大小
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public long GetFileSize(string filename)
        {
            FtpWebRequest reqFTP;
            long fileSize = 0;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
       
;     reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                fileSize = response.ContentLength;
                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileSize Error --> " + ex.Message);
            }
            return fileSize;
        }
        /// <summary>
        /// 改名
        /// </summary>
        /// <param name="currentFilename"></param>
        /// <param name="newFilename"></param>
        public void ReName(string currentFilename, string newFilename)
        {
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename));
                reqFTP.Method = WebRequestMethods.Ftp.Rename;
                reqFTP.RenameTo = newFilename;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Insert_Standard_ErrorLog.Insert("FtpWeb", "ReName Error --> " + ex.Message);
            }
        }
        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="currentFilename"></param>
        /// <param name="newFilename"></param>
        public void MovieFile(string currentFilename, string newDirectory)
        {
            ReName(currentFilename, newDirectory);
        }
        /// <summary>
        /// 切换当前目录
        /// </summary>
        /// <param name="DirectoryName"></param>
        /// <param name="IsRoot">true 绝对路径   false 相对路径</param>
        public void GotoDirectory(string DirectoryName, bool IsRoot)
        {
            if (IsRoot)
            {
                ftpRemotePath = DirectoryName;
            }
            else
            {
                ftpRemotePath += DirectoryName + "/";
            }
            ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
        }
    }
    public class Insert_Standard_ErrorLog
    {
        public static void Insert(string x, string y )
        {
        }
    }
        
}

OK,今天就说到这里,留点精力研究大数据。

发表评论
匿名  
用户评论

匿名游客

2018年11月16日 11:45
写的这儿是啥呢么,我滴神

匿名游客

2018年11月13日 18:11
这篇文章真的非常精彩,解决了我的大问题

匿名游客

2018年11月12日 20:01
这骗文章写的很不错,加油楼主在哪个公司