“大规模杀伤性武器”网络 Markdown 和如何实现图像上传功能

发布于 2024-11-29 07:53:20 字数 238 浏览 1 评论 0原文

我正在开发一个使用 MarkDown 和 WMD javascript 工具的网站,该工具目前可以满足已经托管的图像的需求,但我希望能够根据 Stack Overflow 在 WMD 中提供图像上传工具,但我不知道怎么办呢?

有谁知道如何实施?我的网站正在使用 ASP.NET MVC 进行开发,我对上传图像等的服务器端很满意,但如何将其挂接到 javascript WMD 编辑器(以及可能的 AJAX 元素)中是我需要考虑的问题。我被困住了。

I'm developing a site that uses MarkDown and WMD javascript tools which currently caters for images that are already hosted, but I'd like to be able to offer an image upload facility within WMD, as per Stack Overflow, but don't know how to go about this.

Does anyone know how this can be implemented? My site is being developed, using ASP.NET MVC, and I'm fine with the server side of uploading an image etc, but it's how to hook this into the the javascript WMD editor (and potentially the AJAX element of it) that I'm stuck on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-12-06 07:53:20

我们最近发布了 WMD 的重构版本;您可以在 http://code.google.com/p/pagedown/ 找到它。

Stack Overflow 使用的所有非标准内容现在都通过插件挂钩进行,其中包括我们的文件上传器。所以你可以使用相同的钩子来实现这一点。查看文档; “连接”部分归结为:

editor.hooks.set("insertImageDialog", function (callback) {
    var dia = createMyDialog();
    dia.find(".ok-button").click(function () {
        var url = getChosenImageUrl();
        removeMyDialog();
        callback(url);
    });
    dia.find(".cancel-button").click(function () {
        removeMyDialog();
        callback(null);
    });
    return true; // tell the editor not to show the standard dialog
});

至于实际的上传,我们目前使用一个非常丑陋的解决方案,其工作原理如下:

  • 实际的文件上传表单将其 target 属性设置为隐藏的 iframe ,因此提交表单不会将您带到不同的页面。
  • 在全局对象上定义了一个函数,用于删除对话框并调用回调。这是丑陋的部分;这种方法一点也不干净,但效果很好。
  • 上传控制器操作返回一个最小的 HTML 文档,其中包含一段调用此函数的 JavaScript 代码(通过 window.parent,因为我们必须突破 iframe - 请注意,这需要上传 URL与页面具有相同的起源!)以及新创建图像的地址。

如果您打算以类似的方式实现这一点,请查看 Meta Stack Overflow 上的这篇文章,涉及可能出现的令人讨厌的 Chrome 错误及其解决方法。

We have recently released our refactored version of WMD; you can find it at http://code.google.com/p/pagedown/.

Everything that is non-standard about Stack Overflow's usage now happens via plugin hooks, and this includes our file uploader. So you can just use the same hook to implement this. Have a look at the documentation; the "hooking up" part boils down to this:

editor.hooks.set("insertImageDialog", function (callback) {
    var dia = createMyDialog();
    dia.find(".ok-button").click(function () {
        var url = getChosenImageUrl();
        removeMyDialog();
        callback(url);
    });
    dia.find(".cancel-button").click(function () {
        removeMyDialog();
        callback(null);
    });
    return true; // tell the editor not to show the standard dialog
});

As to the actual uploading, we currently use a pretty ugly solution that works like this:

  • The actual file upload form has its target attribute set to a hidden iframe, so submitting the form doesn't send you to a different page.
  • There's a function defined on the global object that removes the dialog and calls the callback. This is the ugly part; this method is anything but clean, but it works fine.
  • The upload controller action returns a minimal HTML document with a piece of JavaScript code that calls this function (via window.parent, since we have to break out of the iframe – note that this requires the upload URL to have the same origin as the page!) with the address of the newly created image.

If you're going to implement this in a similar fashion, take a look at this post on Meta Stack Overflow regarding a nasty Chrome bug that can appear, and a workaround for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文