jquery - 如何在 webmatrix(ASP.NET 网页)中使用 jQuery 文件上传?

标签 jquery file-upload webmatrix

我想在我的网站上使用此 jQuery 文件上传 https://github.com/blueimp/jQuery-File-Upload根据文档,我需要创建自己的文件上传处理程序,我想知道是否有人有在 webmatrix 站点中使用 jQuery 文件上传的经验?

而且,我找不到创建自己的文件上传处理程序所需的信息。如果有人可以提供帮助,那就太好了。

最佳答案

我可以帮助你,我为我创建的网站创建了一个文件(图像) uploader 。

CSHTML 部分 (sube_archivos.cshtml):

@{
    if (IsPost)
    {
        var fileName = Path.GetFileName(Request.Files["archivo"].FileName);
        var fileSavePath = Server.MapPath("~/Temp/" + fileName);
        try
        {
            Request.Files["archivo"].SaveAs(fileSavePath);

            <img src="~/Temp/@fileName" /> @*you could comment this line...it was the succesful response...an image preview*@

        }
        catch (Exception)
        {
            <text>Error en la subida del archivo</text> @*you could comment this line...error in the uploading*@
        }
    }
}

JQUERY/JAVASCRIPT 部分:

function sube(archivo) {
  var primer_archivo = archivo.files[0];
  var data = new FormData();
  data.append('archivo', primer_archivo);
  $.ajax({
    url: "@Href("../../operaciones/sube_archivos.cshtml")",
    type: "POST",
    contentType: false,
    data: data,
    processData: false,
    cache: false,
    success: function (response) {

      //here I put the response ....the image preview or the error message
      $(archivo).parent().next('.imagen_cargada').html(response);

      //here I get the file name and I add it to some specific div
      $(archivo).parent().next().next('.nombre_imagen').val($(archivo).val().split('\\').pop());
    }
  });
}

HTML 部分:

    <form method="post" enctype="multipart/form-data">
      <input type="file" name="archivo" onchange="sube(this)" />
    </form>

祝你好运,如果有任何不清楚的地方请告诉我。

关于jquery - 如何在 webmatrix(ASP.NET 网页)中使用 jQuery 文件上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072762/

相关文章:

c# - Linq to SQL for WebMatrix(网页)

javascript - 通过 jQuery 插入 HTML 不起作用

javascript - 在 jQuery Mobile 中制作自定义背景图片

jquery - 将欧芹与 Select2 一起使用

jquery - 表单内表单序列化问题

php - 此代码方法中的安全缺陷

java - 使用 GWT 上传和下载文件

javascript - dropzone.js php 上传不起作用

asp.net - 开发 ASP.NET 网站的最佳平台 Visual Web Express 2010 或 WebMatrix

c# - 如何从 jQuery AJAX 调用 WebMatrix Helper 函数?