C# 文件流文件保存
我在画布元素中有一个图像文件,我在asp.net的代码中得到了它。现在我想将它保存到项目中的一个文件夹中,但文件流总是将它保存到c驱动器中。我该怎么办C# 文件流文件保存,c#,asp.net,C#,Asp.net,我在画布元素中有一个图像文件,我在asp.net的代码中得到了它。现在我想将它保存到项目中的一个文件夹中,但文件流总是将它保存到c驱动器中。我该怎么办 [WebMethod()] 公共void SaveUser(字符串imageData) { //在本地计算机上创建映像。 字符串fileNameWitPath=path+“4200020789506”+“.png”; 使用(FileStream fs=newfilestream(fileNameWitPath,FileMode.Create))
[WebMethod()]
公共void SaveUser(字符串imageData)
{
//在本地计算机上创建映像。
字符串fileNameWitPath=path+“4200020789506”+“.png”;
使用(FileStream fs=newfilestream(fileNameWitPath,FileMode.Create))
{
使用(BinaryWriter bw=新的BinaryWriter(fs))
{
字节[]数据=Convert.FromBase64String(imageData);
写入(数据);
bw.Close();
}
}
//将fileNameWitPath变量保存到数据库。
}
我只能想象你的路径
变量指向你的C:\驱动器
您需要将path变量设置为所需的位置,例如:
public void SaveUser(string imageData)
{
path = @"C:\YourCustomFolder\"; // your path needs to point to the Directory you want to save
//Create image to local machine.
string fileNameWitPath = path + "4200020789506" + ".png";
//chekc if directory exist, if not, create
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
// Save fileNameWitPath variable to Database.
}
我还包括一个检查,看看你的目录是否存在,如果不存在,它将在你的C驱动器上创建一个名为“YourCustomFolder”的文件夹,在那里它将保存图像
如果您想将图像保存到项目中的文件夹中,我建议使用
Server.MapPath(~/YourFolderInApplication)
我只能想象您的path
变量指向C:\驱动器
您需要将path变量设置为所需的位置,例如:
public void SaveUser(string imageData)
{
path = @"C:\YourCustomFolder\"; // your path needs to point to the Directory you want to save
//Create image to local machine.
string fileNameWitPath = path + "4200020789506" + ".png";
//chekc if directory exist, if not, create
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
// Save fileNameWitPath variable to Database.
}
我还包括一个检查,看看你的目录是否存在,如果不存在,它将在你的C驱动器上创建一个名为“YourCustomFolder”的文件夹,在那里它将保存图像
如果要将图像保存到项目中的文件夹中,我建议使用
Server.MapPath(~/YourFolderInApplication)
以下是如何将文件保存到项目目录中的Images
文件夹的示例
var fileName = "4200020789506.png";
var base64String = SOME_REALLY_LONG_STRING;
using (var s = new MemoryStream(Convert.FromBase64String(base64String)))
using (var f = new FileStream(Path.Combine(Server.MapPath("~/Images"), fileName), FileMode.Create, FileAccess.Write))
{
s.CopyTo(f);
}
下面是我如何将文件保存到项目目录中的
Images
文件夹的示例
var fileName = "4200020789506.png";
var base64String = SOME_REALLY_LONG_STRING;
using (var s = new MemoryStream(Convert.FromBase64String(base64String)))
using (var f = new FileStream(Path.Combine(Server.MapPath("~/Images"), fileName), FileMode.Create, FileAccess.Write))
{
s.CopyTo(f);
}
我就是这么做的,而且效果很好。对于您,filePath/filename=fileNameWitPath。对您拥有的每个文件执行此操作。希望对你有用。如果你需要更多的信息,我很乐意帮助你
using (var stream = File.Create(filePath + filename))
{
attachment.ContentObject.DecodeTo(stream, cancel.Token);
}
我就是这么做的,而且效果很好。对于您,filePath/filename=fileNameWitPath。对您拥有的每个文件执行此操作。希望对你有用。如果你需要更多的信息,我很乐意帮助你
using (var stream = File.Create(filePath + filename))
{
attachment.ContentObject.DecodeTo(stream, cancel.Token);
}
path
是否有值?yes path有一个指向C:\Demo文件夹的值是C:\Demo
还是C:\Demo\
?这可能是您直接保存到C的原因:它的@“C:\Demo\”我想将它保存在我的visual studio项目文件夹中的img文件夹中,而且如果我传递保存在字符串变量中的值,而不是直接传递“4200020789506”它没有给出任何错误,但也不会保存图像:oooooooo您可能需要查看下面答案中提到的Server.MapPath
方法(只是不要使用fileNameWitPath,只使用文件名)path
是否有值?yes path有一个指向C:\Demo文件夹的值是C:\Demo
还是C:\Demo\
?这可能是您直接保存到C的原因:它的@“C:\Demo\”我想将它保存在我的visual studio项目文件夹中的img文件夹中,而且如果我传递保存在字符串变量中的值,而不是直接传递“4200020789506”它没有给出任何错误,但也不会保存图像:oooooooo您可能需要查看下面答案中提到的Server.MapPath
方法(只是不要使用fileNameWitPath,只使用文件名)