首先,我们先来定义一个类UpLoadFile,这个类就是文件上传类。代码如下:
public void UpLoadFile(string fileNamePath, string uriString, bool IsAutoRename)
{
int indexOf = 0
if (fileNamePath.Contains(@"\"))
{
indexOf = fileNamePath.LastIndexOf(@"\")
}
else if (fileNamePath.Contains("/"))
{
indexOf = fileNamePath.LastIndexOf("/")
}
string fileName = fileNamePath.Substring(indexOf + 1)
string NewFileName = fileName
if (IsAutoRename)
{
NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."))
}
string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1)
if (uriString.EndsWith("/") == false) uriString = uriString + "/"
uriString = uriString + NewFileName
/// 创建WebClient实例
WebClient myWebClient = new WebClient()
myWebClient.Credentials = CredentialCache.DefaultCredentials
// 要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read)
//FileStream fs = OpenFile()
BinaryReader r = new BinaryReader(fs)
byte[] postArray = r.ReadBytes((int)fs.Length)
Stream postStream = myWebClient.OpenWrite(uriString, "PUT")
response = requests.post(PATH, data={'document_name': document_path, 'file': base64.b64encode(file.read())}) 这个方法其实就是将文件信息和转成base64的文件发到服务器那边,那边就是以下的方法存入服务器
文件存入本地的方法
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)