jueves, 26 de octubre de 2023

C# ASP.NET - Subir archivos a carpeta compartida con credenciales.

 .ASPX

<%@ Page Title="Test Carpeta Compartida" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="TestCarpetaCompartida.aspx.cs" Inherits="LeaseOperWeb.Paginas.Administracion.TestCarpetaCompartida.TestCarpetaCompartida" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HeaderContent" runat="server">

    <br /><asp:Label ID="Label1" runat="server" Text="TEST Carpeta Compartida"></asp:Label>

    <br />

    <br />

    <asp:FileUpload ID="fileUpload" runat="server" />

    <asp:Button ID="uploadButton" runat="server" Text="Subir archivo" OnClick="UploadFile" />

    <br />

    <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

</asp:Content>



.CS

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LeaOperBussinesLayer.BussinesComponents.Administracion;

namespace LeaseOperWeb.Paginas.Administracion.TestCarpetaCompartida
{
    public partial class TestCarpetaCompartida : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadFile(object sender, EventArgs e)
        {
            string urlCarpetaCompartida = new TesteoBc().GetURLCarpetaCompartida();
            string usernameCarpetaCompartida = new TesteoBc().GetUserCarpetaCompartida();
            string passwordCarpetaCompartida = new TesteoBc().GetPassCarpetaCompartida();


            if (!String.IsNullOrEmpty(urlCarpetaCompartida) || !String.IsNullOrEmpty(usernameCarpetaCompartida) ||
                !String.IsNullOrEmpty(passwordCarpetaCompartida))
            {
                if (fileUpload.HasFile)
                {
                    string fileName = Path.GetFileName(fileUpload.FileName);

                    string filePath =
                        Server.MapPath($"~/Uploads/{fileName}"); // Ruta temporal para guardar el archivo
                    string folderPath = Server.MapPath("~/Uploads");

                    if (!Directory.Exists(folderPath))
                        Directory.CreateDirectory(folderPath);

                    // Eliminar el archivo temporal en caso de que exista previamente
                    if (File.Exists(filePath))
                        File.Delete(filePath);

                    fileUpload.SaveAs(filePath);

                    // string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);
                    string sharedFolderPath = urlCarpetaCompartida;

                    try
                    {
                        using (FileStream fileStream = File.OpenRead(filePath))
                            if (sharedFolderPath != null)
                                using (FileStream destination = File.Create(Path.Combine(sharedFolderPath, fileName)))
                                {
                                    fileStream.CopyTo(destination);
                                }

                        lblMessage.Text = @"Archivo subido exitosamente";
                    }
                    catch (Exception ex)
                    {
                        lblMessage.Text = @"Ha ocurrido un error: " + ex.Message;
                    }
                }
            }
            else
            {
                lblMessage.Text =
                    @"Atención : Revisar paramétros 908-909-910 alguno de esos está vacío";
            }
        }
    }
}

No hay comentarios:

Publicar un comentario