日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

c winform 上传文件到mysql_WinForm上传文件至服务器

發(fā)布時間:2025/3/20 数据库 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c winform 上传文件到mysql_WinForm上传文件至服务器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

///

/// WebClient上傳文件至服務(wù)器

///

/// 文件名,全路徑格式

/// 服務(wù)器文件夾路徑

///

public bool Upload(string localFilePath, out string folderName,string newFileName)

{

//先創(chuàng)建文件夾

folderName = "";

try

{

Guid guid = Guid.NewGuid();

folderName = guid.ToString();

string diskPath = DAL.DataBaseOperator.GetValueFromApplictionConfig("diskPath");

if (!diskPath.EndsWith("/") && !diskPath.EndsWith(@"\"))

{

diskPath = diskPath + "/";

}

diskPath += folderName;

if (!Directory.Exists(diskPath))

{

//服務(wù)器創(chuàng)建文件夾

Directory.CreateDirectory(diskPath);

}

//再上傳數(shù)據(jù)

string serverFolder = DAL.DataBaseOperator.GetValueFromApplictionConfig("uploadPath");

if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith(@"\"))

{

serverFolder = serverFolder + "/";

}

string uriString = serverFolder + folderName + "/" + newFileName;

/// 創(chuàng)建WebClient實例

WebClient myWebClient = new WebClient();

myWebClient.Credentials = CredentialCache.DefaultCredentials;

// 要上傳的文件

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

//判斷文件大小

string strFileSize = DAL.DataBaseOperator.GetValueFromApplictionConfig("fileSize");

int fileSize = Convert.ToInt32(strFileSize) * 1024 * 1024;

if (fs.Length > fileSize)

{

MessageBox.Show("您上傳的附件不能超過 " + strFileSize + "M");

return false;

}

BinaryReader r = new BinaryReader(fs);

//使用UploadFile方法可以用下面的格式

myWebClient.UploadFile(uriString,"PUT",localFilePath);

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

Stream postStream = myWebClient.OpenWrite(uriString, "PUT");

if (postStream.CanWrite)

{

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

}

else

{

MessageBox.Show("文件目前不可寫!");

}

Application.DoEvents();

postStream.Close();

}

catch(Exception err)

{

//MessageBox.Show("文件上傳失敗,請稍候重試~");

DAL.Log.FileLogSys.FileLog.WriteLog(err.Message + err.StackTrace);

return false;

}

return true;

}

總結(jié)

以上是生活随笔為你收集整理的c winform 上传文件到mysql_WinForm上传文件至服务器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。