//WebClient上传音频文件
public string UploadVoice(string fileNamePath)
{
Voice model=new Voice();
string saveUrl="D:/test";
string fileName=fileNamePath.Substring(fileNamePath.LastIndexOf("\")+1);
if(saveUrl.EndsWith("/")==false)
{
saveUrl=saveUrl+"/";
}
saveUrl=saveUrl+fileName;
//创建WebClient实例
WebClient myWebClient=new WebClient();
myWebClient.Credentials=CredentialCache.DefaultCredentials;
//要上传的文件
FileStream fs=new FileStream(fileNamePath,FileMode.Open,FileAccess.Read);
BinaryReader r=new BinaryReader(fs);
try
{
model.Name=fileName;
model.Path=saveUrl;
db.Voice.Add(model);
db.SaveChanges();
byte[] postArray=r.ReadBytes((int)fs.Length);
Stream postStream=myWebClient.OpenWrite(saveUrl,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
}
else
{
return "No Write.";
}
postStream.Close();
return fileName;
}
catch(Exception ex)
{
return "Error:"+ex.Message;
}
}