c#mvc上传文件保存到后台_C# MVC实现前端上传文件保存到服务器

后台代码

public string UploadECCInvoice_Intranet()

{

//返回前端结果状态对象

StateClass sc = new StateClass();

int resultZT = -1;

HttpPostedFileBase file2 = Request.Files[0];

HttpPostedFileBase file = Request.Files[0];

Stream fileStr = file.InputStream;

string fileName = file.FileName;

string path_ECCInvoice = new FileInfo(fileName).FullName;

sc.ztValues = resultZT;

try

{

string destinationFile = path_ECCInvoice;//要上传的文件

bool status = false;

//连接

string serverFolder = @"\\192.168.3.66\电子竞价图纸";

string PWD = "qwer.5678";//密码

status = connectState(serverFolder, "ls", PWD);

//status = connectState(serverFolder, "Everyone", "");

if (status)

{

//共享文件夹的目录

DirectoryInfo theFolder = new DirectoryInfo(serverFolder);

string filename = theFolder.ToString();

//执行上传方法

//resultZT = UpLoadFiles(destinationFile, serverFolder, "ls", PWD);

sc = UpLoadFiles(file2, serverFolder, "ls", PWD);

}

}

catch (Exception ex)

{

}

return sc.ToJson();

}

/// /// 连接远程共享文件夹

///

/// 远程共享文件夹的路径

/// 用户名

/// 密码

///

public static bool connectState(string path, string userName, string passWord)

{

bool Flag = false;

Process proc = new Process();

try

{

proc.StartInfo.FileName = "cmd.exe";

proc.StartInfo.UseShellExecute = false;

proc.StartInfo.RedirectStandardInput = true;

proc.StartInfo.RedirectStandardOutput = true;

proc.StartInfo.RedirectStandardError = true;

proc.StartInfo.CreateNoWindow = true;

proc.Start();

proc.StandardInput.WriteLine("net use * /del /y");

string dosLine = "net use " + path + " " + passWord + " /user:" + userName;

proc.StandardInput.WriteLine(dosLine);

proc.StandardInput.WriteLine("exit");

while (!proc.HasExited)

{

proc.WaitForExit(1000);

}

string errormsg = proc.StandardError.ReadToEnd();

proc.StandardError.Close();

if (string.IsNullOrEmpty(errormsg))

{

Flag = true;

}

else

{

throw new Exception(errormsg);

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

proc.Close();

proc.Dispose();

}

return Flag;

}

public static StateClass UpLoadFiles(HttpPostedFileBase file, string urlPath, string User, string Pwd)

{

StateClass sate = new StateClass();

int values = 99;

sate.ztValues = values;

try

{

//string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"\") + 1);//取文件名称

//if (urlPath.EndsWith(@"\") == false) urlPath = urlPath + @"\";

//urlPath = urlPath + newFileName;

WebClient myWebClient = new WebClient();

NetworkCredential cread = new NetworkCredential();

Stream fileStr = file.InputStream;

urlPath = urlPath +"\\"+ file.FileName;

myWebClient.Credentials = cread;

//FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);

BinaryReader r = new BinaryReader(fileStr);

try

{

byte[] postArray = r.ReadBytes((int)fileStr.Length);

Stream postStream = myWebClient.OpenWrite(urlPath);

if (postStream.CanWrite)

{

postStream.Write(postArray, 0, postArray.Length);

sate.ztValues = 0;

sate.massege = "file:" + urlPath;

}

else

{

}

postStream.Close();

}

catch (Exception ex)

{

}

}

catch (Exception ex)

{

}

return sate;

}

前端代码

JS

//添加时附件上传

function updateLableFile(_this) {

//alert($(_this).filebox('getValue'));

var _imgsrc = $(_this).filebox('getValue');

console.log(_imgsrc);

var _file = $(_this).context.ownerDocument.activeElement.files[0];

console.log(_file);

var _fileUrl = getObjectUrl(_file);

console.log(_fileUrl);

_file.url = _fileUrl;

var _fileName = _file.name;

var fileType = _fileName.substr(_fileName.lastIndexOf("."));

if (fileType != '.jpg' && fileType != '.png' && fileType != '.gif' && fileType != '.pdf') {

$.messager.alert('提示信息', '请上传正确的附件', 'error');

$(_this).val("")

return false;

} else {

var formData = new FormData();

formData.append("files", _file);

debugger

$.ajax({

url: app.opt.url.inFile,

type: 'POST',

cache: false,

data: formData,

dataType: "json",

processData: false,

contentType: false,

beforeSend: function(){

modILload('数据正在上传中');

},

success: function (data) {

modilDisLoad();

debugger

if (data == '0') {

$.messager.alert('提示信息', '导入成功!', 'info');

} else {

$.messager.alert('提示信息', '导入失败', 'info');

}

}, error: function () {

modilDisLoad();

$.messager.alert('提示信息', '服务器错误!', 'info');

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值