如何在共享驱动器中上传图像和文档? [英] how to upload images and documents in shared drive?

查看:55
本文介绍了如何在共享驱动器中上传图像和文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在共享驱动器中添加图像和文档。也想从共享驱动器查看此文档。

我该怎么办?

现在我上传这个图像和文件只在应用程序中如何更改此代码请帮助我。

我的代码在这里:

Hi i want to add images and document in shared drive & also want to view this document from shared drive.
How can i do?
now i am upload this images and documents in application only how to change this code please help me.
my code is here:

protected void btnUpLoad_Click(object sender, EventArgs e)
   {
       try
       {
           mydt = (DataTable)(ViewState["mydatatable"]);
           //DataRow myrow = mydt.NewRow();
           DataRow myrow = mydt.NewRow();
           int fileSize = fuDocument.PostedFile.ContentLength;
           if (fuDocument.HasFile && fileSize > 0)
           {
               string ext = fuDocument.FileName.Substring(fuDocument.FileName.LastIndexOf(".")).ToLower();
               if (ext == ".doc" || ext == ".txt" || ext == ".pdf" || ext == ".xls" || ext == ".jpg" || ext == ".gif" || ext == ".bmp" || ext == ".png" || ext == ".jpg")
               {

                  string Filename = fuDocument.FileName;
                  string DocumentNameDoc = (emp_seq + myrow["Month"]);
                  url = DocumentNameDoc;
                   //Session["Doc"] = url;
                  ViewState["Doc"] = url;
                  doc = ViewState["Doc"].ToString();
                   //FtpURL = "192.168.1.3";
                   //string FtpUserID = "keshav";
                   //string Ftppassword = "abc@789";
                   string url1 = "..\\Images\\Emp Docs\\" + url;
                   fuDocument.PostedFile.SaveAs(Server.MapPath("..\\Images\\Emp Docs\\" + url));

               }

           }
           else
           {
               msgDiv.InnerHtml = "";
               msgDiv.InnerHtml = "Please upload correct format";
           }



           myrow["Emp_Code"] = txtEmpCode.Text.Trim();
           myrow["Employee_Name"] = txtname.Text.Trim();
           myrow["Salary_Slip_No"] = txtsalary.Text;
           if (txtSubmitDate.Text.Length == 0)
           {
               myrow["Submit_Date"] = DBNull.Value;

           }
           else
           {
               myrow["Submit_Date"] = txtSubmitDate.Text.Trim();
           }

           myrow["Month"] = drdwnmonth.SelectedItem.Text;
           myrow["Salary_URL"] = url;
           mydt.Rows.Add(myrow);
           ViewState["mydatatable"] = mydt;
           if (mydt.Rows.Count > 0)
           {
               gvDocDetails.DataSource = mydt;
               gvDocDetails.DataBind();
               gvDocDetails.Columns[13].Visible = false;
               gvDocDetails.Columns[14].Visible = false;
               gvDocDetails.Columns[15].Visible = false;
               gvDocDetails.Columns[16].Visible = false;
               gvDocDetails.Columns[17].Visible = false;
               gvDocDetails.Columns[18].Visible = false;
               btnupdate.Visible = false;
               btnUpLoad.Visible = true;
               //btnSave.Visible = true;
               btnCancel.Visible = true;
               drdwnmonth.SelectedIndex = 0;
               txtEmpCode.Text = "";
               txtname.Text = "";
               txtsalary.Text = "";
               txtSubmitDate.Text = "";
           }
           else
           {
               msgDiv.InnerHtml = "";
               msgDiv.InnerHtml = "No Rows Added";
           }
       }
       catch (Exception ex)
       {
           // msgDiv.InnerHtml = ex.Message;
       }

      // DirectoryCopy(".", @".\temp", true);
   }

推荐答案

我遇到类似情况,请找到解决我问题的代码



I was in similar situation, please find the code which solved my issue

public class NetworkConnection : IDisposable
  {
      private static readonly ILog objLogger = LogManager.GetLogger(typeof(NetworkConnection));
      string _networkName;

      public NetworkConnection(string networkName,
          NetworkCredential credentials)
      {
          _networkName = networkName;
          objLogger.Info(_networkName);

          var netResource = new NetResource()
          {
              Scope = ResourceScope.GlobalNetwork,
              ResourceType = ResourceType.Disk,
              DisplayType = ResourceDisplaytype.Share,
              RemoteName = networkName
          };

          objLogger.Info(netResource);

          var userName = string.IsNullOrEmpty(credentials.Domain)
              ? credentials.UserName
              : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

          var result = WNetAddConnection2(
              netResource,
              credentials.Password,
              userName,
              1);

          objLogger.Info("Result: " + result);

          if (result != 0)
          {
              throw new System.ComponentModel.Win32Exception(result, "Error connecting to remote share" + credentials.Password.ToString() + "--" + credentials.Domain + credentials.UserName);
          }
      }

      ~NetworkConnection()
      {
          Dispose(false);
      }

      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }

      protected virtual void Dispose(bool disposing)
      {
          WNetCancelConnection2(_networkName, 0, true);
      }

      [DllImport("mpr.dll")]
      private static extern int WNetAddConnection2(NetResource netResource,
          string password, string username, int flags);

      [DllImport("mpr.dll")]
      private static extern int WNetCancelConnection2(string name, int flags,
          bool force);
  }

  [StructLayout(LayoutKind.Sequential)]
  public class NetResource
  {
      public ResourceScope Scope;
      public ResourceType ResourceType;
      public ResourceDisplaytype DisplayType;
      public int Usage;
      public string LocalName;
      public string RemoteName;
      public string Comment;
      public string Provider;
  }

  public enum ResourceScope : int
  {
      Connected = 1,
      GlobalNetwork,
      Remembered,
      Recent,
      Context
  };

  public enum ResourceType : int
  {
      Any = 0,
      Disk = 1,
      Print = 2,
      Reserved = 8,
  }

  public enum ResourceDisplaytype : int
  {
      Generic = 0x0,
      Domain = 0x01,
      Server = 0x02,
      Share = 0x03,
      File = 0x04,
      Group = 0x05,
      Network = 0x06,
      Root = 0x07,
      Shareadmin = 0x08,
      Directory = 0x09,
      Tree = 0x0a,
      Ndscontainer = 0x0b
  }







使用主页上面的课程




Using the above class in main page

 NetworkCredential NCredentials = new NetworkCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);

 using (new NetworkConnection(@"" + ConfigurationManager.AppSettings["Networkpath"].ToString(), NCredentials))
                        {
                       
  if (Directory.Exists(FPath + monthName))
                            {
                                FPath = FPath + monthName + "\\" + FileName;
                                FileStream fs = new FileStream(FPath, FileMode.Create);
                                fs.Write(results, 0, results.Length);
                                fs.Close();
                            }
                            else
                            {

                                Directory.CreateDirectory(FPath + monthName);
                                FPath = FPath + monthName + "\\" + FileName;
                                FileStream fs = new FileStream(FPath, FileMode.Create);
                                fs.Write(results, 0, results.Length);
                                fs.Close();
                            }

}


这篇关于如何在共享驱动器中上传图像和文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆