C# winform上传,删除办事器文件

Emerson墨 2012-11-29 03:29:21
今天在winform上须要上传,删除办事器上的文件,网上找了下上传是很多,可删除没有找到答案,,最后只能拿个土办法实现了,这里是用ashx文件来处理惩罚

上传:

B/S



public void ProcessRequest(HttpContext context)
{
foreach (string fileKey in context.Request.Files)
{
try
{
HttpPostedFile file = context.Request.Files[fileKey];
string oldFileName = file.FileName; //原文件名 xxx.jpg
int extenIndex = oldFileName.LastIndexOf(".");
string getExtent = oldFileName.Substring(extenIndex);//文件扩大名 .jpg
string fileSize = UploadCommon.getFileSize(file.ContentLength); //获取文件大小
string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + (new Random()).Next(0, 9) + getExtent;
string path = "/AffixFiles/sw_Products/";
if (!Directory.Exists(context.Server.MapPath(path)))
{
Directory.CreateDirectory(context.Server.MapPath(path));
}
string filePath = Path.Combine(path, newFileName);//文件相对路径
file.SaveAs(context.Server.MapPath(filePath));
string json = "{\"status\":\"success\",\"oldFileName\":\"" + oldFileName + "\",\"fileSize\":\"" + fileSize + "\",\"filePath\":\"" + filePath + "\",\"fileType\":\"" + getExtent + "\"}";
context.Response.Write(json);
}
catch
{
string json = "{\"status\":\"error\",\"oldFileName\":\"" + null + "\",\"fileSize\":\"" + null + "\",\"filePath\":\"" + null + "\",\"fileType\":\"" + null + "\"}";
context.Response.Write(json);
}
context.Response.End();
}
}

C/S



private void 上传_Click(object sender, EventArgs e)
{
System.Net.WebClient myWebClient = new System.Net.WebClient();
byte[] b = myWebClient.UploadFile("http://190.160.10.13:8088/ClientUpload/ClientUploadTools.ashx",
"POST", "C:\\1351040562_Download.png");

string returnJson = Encoding.Default.GetString(b);
JavaScriptSerializer json = new JavaScriptSerializer();
uploadMessage result = json.Deserialize<uploadMessage>(returnJson);//最后的返回成果集

}
public class uploadMessage
{
public string oldFileName { get; set; }
public string fileSize { get; set; }
public string filePath { get; set; }
public string fileType { get; set; }
public string status { get; set; }
}





删除:

这里删除就应用了 UploadData办法来处理惩罚,太土了,大师有没有更好的办法供给一下哈

B/S



public void ProcessRequest(HttpContext context)
{
tryGoogle
{
int totalBytes = context.Request.TotalBytes;
if (totalBytes > 0)
{
byte[] readBytes = context.Request.BinaryRead(totalBytes);
string filePath = System.Text.Encoding.Default.GetString(readBytes);
string overFile = context.Server.MapPath(filePath);
if (File.Exists(overFile))
{
try
{
File.Delete(overFile);
context.Response.Write("success");
}
catch
{
context.Response.Write("error");
}
}
}
}
catch
{
context.Response.Write("error");
}
context.Response.End();
}




C/S


/// <summary>
/// 删除成功success 删除失败 errorwww.szwawq.com
/// </summary>
private void 删除_Click(object sender, EventArgs e)
{
System.Net.WebClient myWebClient = new System.Net.WebClient();
byte[] b = myWebClient.UploadData("http://localhost:2666/ClientUpload/ClientDeleteTool.ashx",
"POST", Encoding.Default.GetBytes("/AffixFiles/sw_Products/2012112211243824388.png"));
string result = Encoding.Default.GetString(b);//返回的成果
}

请勿夹带广告,否则删帖封号!
...全文
51 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧