返回介绍

保存dwg到服务器

发布于 2023-08-09 23:10:35 字数 4683 浏览 0 评论 0 收藏 0

控件支持在网页中绘图,编辑dwg文件,并把修改后的dwg文件保存到服务器。

SaveDwgToURL

该函数模拟一个文件表单提交,上传文件,在服务器端有个专门的程序来处理文件的上传。

COM接口:

VARIANT_BOOL SaveDwgToURL(  
BSTR pszServerUrl,             //服务器网址地址,如:www.mxdraw.com,如果传空,程序自动取当前网
//页的服务器.
BSTR pszServerProgramUrl,    //服务器的文件上传处理程序,如:upload.asp
BSTR pszComponentName,     //HTML组件名称,相当于一个HTML页面的Form中的中的file1
BSTR pszPort);                  //服务处理端口. 如:_T("80")

详细例程参考 sample/AspNet 中"保存dwg文件到服务器"按钮,向服务器发送的上传文件请求,使用 Fiddler 工具能看见上传数据包, 因为Fiddler的问题, Fiddler工具查看本地服务器请求内容时,本地服务器ip地址需要写成http://127.0.0.1,而不能写成 http://localhost

如下代码调用:

if (!mxOcx.SaveDwgToURL("http://127.0.0.1.", "/Save.aspx", "ComponentName", "6046")) {
       // 得到服务器返回错误
        var ret = MxDrawXCtrl_Obj.Call("Mx_GetLastError","");
        alert(ret.AtString(0));
    }
    else {
        alert("成功");
}

上面代码相当于如下Html代码的调用:

<form action="Save.aspx"method="post" enctype="multipart/form-data">
    <input id="File1" type="file" name="ComponentName" /><br />
    <input id="Submit1" type="submit" value="submit" />
</form>

服务器上,接收上传文件并保存的代码,Asp.net:

namespace AspNet
{
    public partial class Save : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.HttpFileCollection uploadFiles = Request.Files;
            System.Web.HttpPostedFile theFile;
            if (uploadFiles.Count == 0)
            {
                Response.Write("Save failed!!!");
                return;
            }
            else
            {
                try
                {
                    int i = 0;
                    for (; i < uploadFiles.Count; )
                    {
                        theFile = uploadFiles[i];
 
                        // uploadFiles.GetKey(0)对应着
                        // SaveDwgToURL(getcurpath(), "/Save.aspx", "test.dwg","");中的Test.Dwg
                        theFile.SaveAs(Server.MapPath(uploadFiles.GetKey(0)) );
                        Response.Write("Save OK!");
                        i++;
                        return;
                    }
                }
                finally
                {
                    
                }
            }
 
            Response.Write("Save failed!!!");
          
        }
    }
}

例如 ASP.net + MVC调用:

var dwgName = $("#dwgList option:selected").text();
            var hostName = window.location.hostname;
            var port = window.location.port;
            var path = "http://" + hostName + ":" + port;
            alert(hostName+"==="+path);
            if (!mxOcx.SaveDwgToURL(path, "Home/SaveDwgFile/?key=" + Math.random() + "&flag=" + flag, dwgName, ""))
            {
                alert(path);
                var ret = mxOcx.Call("Mx_GetLastError", "");
                alert(ret.AtString(1));
            }
            else
            {
                //alert("保存成功");
            }

服务器上,接收上传文件并保存的代码Servlet:

MxForm mf = (MxForm)actionForm;
        InputStream is = mf.getFile().getInputStream();
        File destFile = new File("D:\civilize\code\web","test.dwg");
        OutputStream os = new FileOutputStream( destFile );
        byte[] buffer = new byte[400];
        int length = 0;
         while( (length = is.read(buffer)) > 0 ) {
            os.write(buffer,0,length);
        }
        mf.getFile().destroy();
        is.close();
        os.close();
        return null;
        // MxForm代码:
        import org.apache.struts.action.ActionForm;
        import org.apache.struts.upload.FormFile;
 
        public class MxForm extends ActionForm {
            private FormFile file;
        public FormFile getFile() {
                return file;
            }
            public void setFile(FormFile file) {
            this.file = file;
            }
        }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文