C#.net中,如何自动保存文件到指定位置
实现以下功能,当点击鼠标时,让服务器上指定文件保存到客户端指定位置,代码怎么写呀?是web的需要在点击后自动保存到客户端指定文件夹下,并且没有会话框提示...
实现以下功能,当点击鼠标时,让服务器上指定文件保存到客户端指定位置,代码怎么写呀?
是web的 需要在点击后自动保存到客户端指定文件夹下,并且没有会话框提示 展开
是web的 需要在点击后自动保存到客户端指定文件夹下,并且没有会话框提示 展开
展开全部
代码示例如下:
//提供下载源地址(在服务器上的地址)
string srcPath = "C:\text.txt";
FileInfo fileInfo = new FileInfo(srcPath);
//若该文件存在则弹出对话框让你选择保存地址(本地)
if (fileInfo.Exists)
{
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
Response.WriteFile(srcPath);
Response.Flush();
Response.Close();
}
补充:不弹出对话框代码如下:
/**//**//**//// <summary>
/**//// 下载文件
/**//// </summary>
/**//// <param name="filename">文件物理地址</param>
protected void DownloadFile(string filename)
...{
string saveFileName = "test.xls";
int intStart = filename.LastIndexOf("\")+1;
saveFileName = filename.Substring(intStart,filename.Length-intStart);
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream(filename,FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
Response.AddHeader("Content-Disposition", "attachment;filename="+saveFileName);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(Buffer);
Response.Flush();
Response.Close();
Response.End();
}
//提供下载源地址(在服务器上的地址)
string srcPath = "C:\text.txt";
FileInfo fileInfo = new FileInfo(srcPath);
//若该文件存在则弹出对话框让你选择保存地址(本地)
if (fileInfo.Exists)
{
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
Response.WriteFile(srcPath);
Response.Flush();
Response.Close();
}
补充:不弹出对话框代码如下:
/**//**//**//// <summary>
/**//// 下载文件
/**//// </summary>
/**//// <param name="filename">文件物理地址</param>
protected void DownloadFile(string filename)
...{
string saveFileName = "test.xls";
int intStart = filename.LastIndexOf("\")+1;
saveFileName = filename.Substring(intStart,filename.Length-intStart);
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream(filename,FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
Response.AddHeader("Content-Disposition", "attachment;filename="+saveFileName);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(Buffer);
Response.Flush();
Response.Close();
Response.End();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是指这个文件在服务器上,点击后保存到客户端? == 下载?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-08-10 · 知道合伙人数码行家
关注
展开全部
截的图保存到指定的路径例子如下:
try
{
Screen scr = Screen.PrimaryScreen;
Rectangle rc = scr.Bounds;
int iWidth = rc.Width;
int iHeight = rc.Height;
Bitmap myImage = new Bitmap(iWidth, iHeight);
Graphics gl = Graphics.FromImage(myImage);
gl.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
_img = myImage;
pictureBox1.Image = _img;
// IntPtr dc1 = gl.GetHdc();
//gl.ReleaseHdc(dc1);
_img.Save(@"C:\\1.jpeg");
//_img.Save("c:\\1.jpeg");
//SendFile("c:\\1.jpeg");
}
catch (Exception ex)
{
MessageBox.Show("截屏失败!\n" + ex.Message.ToString() + "\n" + ex.StackTrace.ToString());
}
解决代码如下:
#region[方法]
///<summary>
///截屏
///</summary>
private void PrintScreen()
{
string Opath = @"C:/Picture";
if (Opath.Substring(Opath.Length - 1, 1) != @"/")
Opath = Opath + @"/";
string photoname = DateTime.Now.Ticks.ToString();
string path1 = Opath + DateTime.Now.ToShortDateString();
if (!Directory.Exists(path1))
Directory.CreateDirectory(path1);
try
{
Screen scr = Screen.PrimaryScreen;
Rectangle rc = scr.Bounds;
int iWidth = rc.Width;
int iHeight = rc.Height;
Bitmap myImage = new Bitmap(iWidth, iHeight);
Graphics gl = Graphics.FromImage(myImage);
gl.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
_img = myImage;
//pictureBox1.Image = _img;
// IntPtr dc1 = gl.GetHdc();
//gl.ReleaseHdc(dc1);
MessageBox.Show(path1);
MessageBox.Show(photoname);
_img.Save(path1 + "//" + photoname + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// _img.Save("D:\\1.jpeg");
SendFile(path1+"//"+photoname+".jpg");
}
catch (Exception ex)
{
MessageBox.Show("截屏失败!\n" + ex.Message.ToString() + "\n" + ex.StackTrace.ToString());
}
// MessageBox.Show("12322222");
/////////////////////////////////////////////////////////
///////////////////发送图片流///////////////////////////
/*
MemoryStream ms = new MemoryStream();
byte[] imagedata = null;
_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imagedata = ms.GetBuffer();
byte[] arrFile = new byte[1024 * 1024 * 2];
//读取文件内容到字节数组,并 获得 实际文件大小
int length = ms.Read(arrFile, 0, arrFile.Length);
// int length = ms.Read(arrFile, 0, arrFile.Length);
//定义一个 新数组,长度为文件实际长度 +1
byte[] arrFileFina = new byte[length + 1];
arrFileFina[0] = 2;//设置 数据标识位等于1,代表 发送的是文件
//将 图片流数据数组 复制到 新数组中,下标从1开始
//arrFile.CopyTo(arrFileFina, 1);
Buffer.BlockCopy(arrFile, 0, arrFileFina, 1, length);
//发送文件数据
sokClient.Send(arrFileFina);//, 0, length + 1, SocketFlags.None);
//MessageBox.Show("我在这里!!!");
// byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(_img);
MessageBox.Show("2222");
*/
}
#endregion
try
{
Screen scr = Screen.PrimaryScreen;
Rectangle rc = scr.Bounds;
int iWidth = rc.Width;
int iHeight = rc.Height;
Bitmap myImage = new Bitmap(iWidth, iHeight);
Graphics gl = Graphics.FromImage(myImage);
gl.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
_img = myImage;
pictureBox1.Image = _img;
// IntPtr dc1 = gl.GetHdc();
//gl.ReleaseHdc(dc1);
_img.Save(@"C:\\1.jpeg");
//_img.Save("c:\\1.jpeg");
//SendFile("c:\\1.jpeg");
}
catch (Exception ex)
{
MessageBox.Show("截屏失败!\n" + ex.Message.ToString() + "\n" + ex.StackTrace.ToString());
}
解决代码如下:
#region[方法]
///<summary>
///截屏
///</summary>
private void PrintScreen()
{
string Opath = @"C:/Picture";
if (Opath.Substring(Opath.Length - 1, 1) != @"/")
Opath = Opath + @"/";
string photoname = DateTime.Now.Ticks.ToString();
string path1 = Opath + DateTime.Now.ToShortDateString();
if (!Directory.Exists(path1))
Directory.CreateDirectory(path1);
try
{
Screen scr = Screen.PrimaryScreen;
Rectangle rc = scr.Bounds;
int iWidth = rc.Width;
int iHeight = rc.Height;
Bitmap myImage = new Bitmap(iWidth, iHeight);
Graphics gl = Graphics.FromImage(myImage);
gl.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
_img = myImage;
//pictureBox1.Image = _img;
// IntPtr dc1 = gl.GetHdc();
//gl.ReleaseHdc(dc1);
MessageBox.Show(path1);
MessageBox.Show(photoname);
_img.Save(path1 + "//" + photoname + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// _img.Save("D:\\1.jpeg");
SendFile(path1+"//"+photoname+".jpg");
}
catch (Exception ex)
{
MessageBox.Show("截屏失败!\n" + ex.Message.ToString() + "\n" + ex.StackTrace.ToString());
}
// MessageBox.Show("12322222");
/////////////////////////////////////////////////////////
///////////////////发送图片流///////////////////////////
/*
MemoryStream ms = new MemoryStream();
byte[] imagedata = null;
_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imagedata = ms.GetBuffer();
byte[] arrFile = new byte[1024 * 1024 * 2];
//读取文件内容到字节数组,并 获得 实际文件大小
int length = ms.Read(arrFile, 0, arrFile.Length);
// int length = ms.Read(arrFile, 0, arrFile.Length);
//定义一个 新数组,长度为文件实际长度 +1
byte[] arrFileFina = new byte[length + 1];
arrFileFina[0] = 2;//设置 数据标识位等于1,代表 发送的是文件
//将 图片流数据数组 复制到 新数组中,下标从1开始
//arrFile.CopyTo(arrFileFina, 1);
Buffer.BlockCopy(arrFile, 0, arrFileFina, 1, length);
//发送文件数据
sokClient.Send(arrFileFina);//, 0, length + 1, SocketFlags.None);
//MessageBox.Show("我在这里!!!");
// byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(_img);
MessageBox.Show("2222");
*/
}
#endregion
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个不好办的,其实说白了就是在下载文件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用HTML超链接不就完了,何必绕一个大圈用C#后台实现呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询