如何将文件从asp.net上传到远程服务器 [英] How to upload files from asp.net to remote server

查看:347
本文介绍了如何将文件从asp.net上传到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,错误是找不到网络路径。

Here is my code and the error is "the network path was not found".

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Wind1
{
    public partial class Form1 : Form
    {
        string fileName = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        private void button1_Click_1(object sender, EventArgs e)
        {
            string path = "";
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Attach customer proposal document";
            fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
            fDialog.InitialDirectory = @"C:\";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = System.IO.Path.GetFileName(fDialog.FileName);
                path = Path.GetDirectoryName(fDialog.FileName);
                textBox1.Text = path + "\\" + fileName;

            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            try
            {
                WebClient client = new WebClient();

                NetworkCredential nc = new NetworkCredential("arcs360", "arcs");

                Uri addy = new Uri(@"\\10.255.1.147\Styles\" + fileName);

                client.Credentials = nc;
                byte[] arrReturn = client.UploadFile(addy,textBox1.Text);
                MessageBox.Show(arrReturn.ToString());

            }
            catch (Exception ex1)
            {
                MessageBox.Show(ex1.Message);
            }
        }
    }
}

推荐答案

尝试使用以下代码块。

希望它能起作用......



Try to use the below code block.
Hope it will work...

private void btnUpload_Click(Object sender, EventArgs e)
{
string strFileName = MyFile.PostedFile.FileName;
 strFileName = System.IO.Path.GetFileName(strFileName);
MyFile.PostedFile.SaveAs(Server.MapPath("servername/files") + strFileName);
 lblMessage.Text = "Your file: " + strFileName + " has been uploaded successfully !";
}


这篇关于如何将文件从asp.net上传到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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