C# 上传新的个人资料图片时,如何删除当前用户添加的以前的图片
我被这些MVC的东西困住了,我怎么能在上传新的个人资料图片时删除旧的个人资料图片 我在sessionId的基础上上传文件,我知道这不是一个好主意,但我如何才能做到这一点 这里是我如何上传的方式,但如何删除新上传的文件C# 上传新的个人资料图片时,如何删除当前用户添加的以前的图片,c#,asp.net-mvc,asp.net-mvc-5.1,C#,Asp.net Mvc,Asp.net Mvc 5.1,我被这些MVC的东西困住了,我怎么能在上传新的个人资料图片时删除旧的个人资料图片 我在sessionId的基础上上传文件,我知道这不是一个好主意,但我如何才能做到这一点 这里是我如何上传的方式,但如何删除新上传的文件 <script type="text/javascript"> var uploader = new qq.FileUploader({ // pass the dom node (ex. $(selector)[0] for jQuery
<script type="text/javascript">
var uploader = new qq.FileUploader({
// pass the dom node (ex. $(selector)[0] for jQuery users)
element: document.getElementById('file-uploader'),
// path to server-side upload script
action: '/User/PictureUpload/',
params: { sessionId: $('#sessionId').val() },
// file extension
allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
messages: {
typeError: "{file} has invalid extension. Only {extensions} are allowed.",
emptyError: "{file} is empty, please select files again without it.",
allowedExtensionsError: "{file} is not allowed.",
onLeave: "The files are being uploaded, if you leave now the upload will be cancelled."
},
showMessage: function (message) {
alert(message);
}
});
//Line for upstairs
///// using (Html.BeginForm(null, null, FormMethod.Post, new {enctype = "multipart/form-data"}))
</script>
[HttpPost]
public ActionResult PictureUpload( string qqfile,string sessionId)
{
var path = Server.MapPath("~/App_data/Files/");
var file = string.Empty;
try
{
var stream = Request.InputStream;
if (String.IsNullOrEmpty(Request["qqfile"]))
{
// IE
HttpPostedFileBase postedFile = Request.Files[0];
if (postedFile != null) stream = postedFile.InputStream;
file = Path.Combine(path, sessionId);
}
else
{
//Webkit, Mozilla
file = Path.Combine(path, sessionId );
}
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
System.IO.File.WriteAllBytes(file, buffer);
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message }, "application/json");
}
return Json(new { success = true }, "text/html");
}
您需要知道旧/当前图片的存储位置,如果上载成功,只需在此处使用以下命令删除旧图片:
stream.Read(buffer, 0, buffer.Length);
System.IO.File.WriteAllBytes(file, buffer);
System.IO.File.Delete(string path);
就像我说的,你必须事先知道当前图片的路径是什么…我试过了,但它说访问路径“C:\Users\jack\Documents\Visual Studio 2012\Projects\A MVC Projects\RSSfeeds\RSSfeeds\App\u data\Files”被拒绝。这很可能是因为你在Visual Studio内部本地运行。。。您还可以先检查文件是否存在。现在请记住,您需要文件的实际路径和文件名,而不仅仅是文件夹。。。由于这是一个.Net站点,我希望图片存储在服务器上,您的应用程序需要对该文件夹具有访问权限才能添加和/或从中删除。