代码之家  ›  专栏  ›  技术社区  ›  mhcodner

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

  •  2
  • mhcodner  · 技术社区  · 13 年前

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

    而且,我找不到创建自己的文件上传处理程序所需的信息。如果有人能帮上忙,那就太棒了。

    2 回复  |  直到 13 年前
        1
  •  1
  •   mjsr    13 年前

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

    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/Java脚本部分:

    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>
    

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

        2
  •  0
  •   Ifeanyi Chukwu    12 年前

    Blueimp是一个痛苦的插件。。。。。。我建议你使用 this 用于表单和文件上传的插件。它的配置更简单且非常灵活。blueimp的问题不仅仅是让插件准备好使用。在asp.net mvc和asp.net webmatrix上,服务器端也很痛苦。