zoukankan      html  css  js  c++  java
  • Winform(C#)——向服务器上传文件代码

    public string  UpLoadFile(string fileNamePath, string uriString, bool IsAutoRename)
            {
                string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\") + 1);
                uriString = "http://192.168.0.62/pdf/";
                string NewFileName = fileName;
                if (IsAutoRename)
                {
                    NewFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
                }

                string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
                if (uriString.EndsWith("/") == false) uriString = uriString + "/";

                uriString = uriString + NewFileName;

                ///  创建WebClient实例 
                WebClient myWebClient = new WebClient();
                myWebClient.Credentials = CredentialCache.DefaultCredentials;

                try
                {
                    // 要上传的文件 
                    FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
                    BinaryReader r = new BinaryReader(fs);
                    byte[] postArray = r.ReadBytes((int)fs.Length);
                    Stream postStream = myWebClient.OpenWrite(uriString, "PUT");

                    try
                    {
                        // 使用UploadFile方法可以用下面的格式
                        
    // myWebClient.UploadFile(uriString,"PUT",fileNamePath); 

                        if (postStream.CanWrite)
                        {
                            postStream.Write(postArray, 0, postArray.Length);
                            postStream.Close();
                            fs.Dispose();
                        }
                        else
                        {
                            postStream.Close();
                            fs.Dispose();
                        }
                    }
                    catch (Exception err)
                    {
                        postStream.Close();
                        fs.Dispose();
                    }
                    finally
                    {
                        postStream.Close();
                        fs.Dispose();
                    }
                    return NewFileName;
                }
                catch
                {
                    ///
                }
                return NewFileName;
            }
  • 相关阅读:
    telnet命令找不到问题
    hive向表中执行insert语句报错问题
    SharePoint 2010 日期控件(DateTimeControl)的用法
    SharePoint 2010 启用InfoPath支持
    Sharepoint 2010 根据用户权限隐藏Ribbon菜单
    Sharepoint 2010 使用feature部署文件
    SharePoint 2010 查询不以某个字符开头的数据[How to Create a SharePoint “Does Not Begin With” Filtered List View]计算栏的妙用
    SharePoint 2010 栏计算经验收集
    SharePoint 2010 更加列表栏的值显示不同的背景颜色
    SharePoint 2010 使用SP.UI.ModalDialog.showModalDialog(options)对话框框架传值
  • 原文地址:https://www.cnblogs.com/bluewhy/p/5242316.html
Copyright © 2011-2022 走看看