我用 stream 上传图片。。之前用好好的。。
但是最近出现问题
说是未能找到路径 "c"\1.jpg" 这个路径是客户端的文件路径
请问是怎么回事?
之前用好好的。。我想应该是权限问题。后来我把目标文件夹 和 c:\windows\temp全加了 everyone权限也还不行。
代码是/// <summary>
/// 通用的上传图片函数
/// </summary>
/// <param name="FileName">需要上传的文件路径(客户端)</param>
/// <param name="path">要上传的地址(服务器端)</param>
/// <param name="authorization">这个不要问。我都用的FALSE</param> /// <returns>上传服务器的文件名</returns>
///
public static string tt66w_UploadPic(string FileName, string path, bool authorization)
{
if (FileName != "")
{
string FilePath = null;
string File_Name = null;
Random rd = new Random();
string NewName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() + rd.Next(100, 1000).ToString();
string fileExtname = FileName.Substring(FileName.LastIndexOf(".") + 1);
fileExtname = fileExtname.ToLower();
if (authorization)
{
return "";
}
else
{
try
{
File_Name = NewName + "." + fileExtname;
FilePath = HttpContext.Current.Server.MapPath(path);
string dir = HttpContext.Current.Server.MapPath(path + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string sdir = HttpContext.Current.Server.MapPath(path + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/");
{
if (!Directory.Exists(sdir))
{
Directory.CreateDirectory(sdir);
}
}
FilePath = dir; WebClient myWebClient = new WebClient();
//设定windows网络安全认证 方法1
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//要上传的文件
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(toFile, "PUT",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
System.IO.Stream postStream = myWebClient.OpenWrite(FilePath + File_Name, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Dispose();
fs.Dispose();
fs.Close();
r.Close();
System.Threading.Thread.Sleep(200);
Lcb.Error.Record("ddddd"); }
catch (Exception ex)
{
Lcb.Error.Record(ex.ToString());
}
return DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + File_Name; }
}
else
{
return "路径错误";
}
}
但是最近出现问题
说是未能找到路径 "c"\1.jpg" 这个路径是客户端的文件路径
请问是怎么回事?
之前用好好的。。我想应该是权限问题。后来我把目标文件夹 和 c:\windows\temp全加了 everyone权限也还不行。
代码是/// <summary>
/// 通用的上传图片函数
/// </summary>
/// <param name="FileName">需要上传的文件路径(客户端)</param>
/// <param name="path">要上传的地址(服务器端)</param>
/// <param name="authorization">这个不要问。我都用的FALSE</param> /// <returns>上传服务器的文件名</returns>
///
public static string tt66w_UploadPic(string FileName, string path, bool authorization)
{
if (FileName != "")
{
string FilePath = null;
string File_Name = null;
Random rd = new Random();
string NewName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() + rd.Next(100, 1000).ToString();
string fileExtname = FileName.Substring(FileName.LastIndexOf(".") + 1);
fileExtname = fileExtname.ToLower();
if (authorization)
{
return "";
}
else
{
try
{
File_Name = NewName + "." + fileExtname;
FilePath = HttpContext.Current.Server.MapPath(path);
string dir = HttpContext.Current.Server.MapPath(path + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string sdir = HttpContext.Current.Server.MapPath(path + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/");
{
if (!Directory.Exists(sdir))
{
Directory.CreateDirectory(sdir);
}
}
FilePath = dir; WebClient myWebClient = new WebClient();
//设定windows网络安全认证 方法1
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//要上传的文件
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(toFile, "PUT",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
System.IO.Stream postStream = myWebClient.OpenWrite(FilePath + File_Name, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Dispose();
fs.Dispose();
fs.Close();
r.Close();
System.Threading.Thread.Sleep(200);
Lcb.Error.Record("ddddd"); }
catch (Exception ex)
{
Lcb.Error.Record(ex.ToString());
}
return DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + File_Name; }
}
else
{
return "路径错误";
}
}
"c"\1.jpg"
应该是
"c:\\1.jpg"
http://www.cnblogs.com/xyd21c/archive/2011/07/14/2106598.html
这个文章。。然后把将FileStream fs = new FileStream(upFileName, FileMode.Open, FileAccess.Read, FileShare.Read);替换为Stream fs = new StreamReader(upFileName);
但是提示 错误 StreamReader无法转换为 stream
但是文章里为什么说要这么换???是不是中间少了几部??????请高手指导孟大哥你来帮帮忙