dsoframer.ocx控件编辑后保存在服务器端的简单方法

        在使用dsoframer.ocx控件的时候经常遇到需要将用户编辑的内容上传到服务器上。控件本身提供的方法是在JS中调用httpPost方法跳转到另一个资源中再进行处理。这种方法使用起来很不方便,下面介绍一种通过struts2带的文件上传方法实现此功能。

 

1、在页面中添加控件以及控件的JS函数。

<script type="text/javascript">
function hide()
{
	document.all.Mydso.Toolbars = false;
	document.all.Mydso.Menubar = false;
	document.all.Mydso.Titlebar = false;
}
function openfile()
{
	var path = document.getElementById('upload').value;
	document.all.Mydso.Open(path,false);
}
function save()
{
	var path = document.getElementById('upload').value;
	document.all.Mydso.Save(path,true);
	return true;
}
</script>

 

2、页面上添加控件以及表单

<body οnlοad="hide()">
<OBJECT 
   classid="clsid:00460182-9E5E-11d5-B7C8-B8269041DD57"
   codeBase="dsoframer.ocx"#Version="2,3,0,0"
   id="Mydso"
          name="Mydso"
   width=600
   height=400
   align=center
   hspace=0
   vspace=0      
>
</OBJECT>
<form action="upload" method="post" enctype="multipart/form-data" οnsubmit="return save()">
	<input type="file" name="upload" id="upload" value="选择文件">
	<input type="button" value="打开文件" οnclick="openfile()">
	<input type="submit" value="提交">
</form>
</body>

body的onload属性调用了hide()方法,目的是隐藏控件的工具栏部分,显得更美观。

表单的action为upload。method必须指定为post,默认的是get。enctype为multipart/form-data。注意:onsubmit为return save()这样在提交表单的时候就会先执行save()函数,如果返回true才提交表单,这就保证了上传的文件为保存后的文件。

 

3、处理文件上传的action

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport
{
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	public File getUpload()
	{
		return upload;
	}
	public void setUpload(File upload)
	{
		this.upload = upload;
	}
	public String getUploadContentType()
	{
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType)
	{
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName()
	{
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName)
	{
		this.uploadFileName = uploadFileName;
	}
	
	@Override
	public String execute() throws Exception
	{
		FileOutputStream fos = new FileOutputStream("d:\\" + getUploadFileName());
		FileInputStream fis = new FileInputStream(getUpload());
		byte[] buffer = new byte[1024];
		int len = 0;
		while((len = fis.read(buffer)) > 0)
		{
			fos.write(buffer, 0, len);
		}
		fos.close();
		fis.close();
		return SUCCESS;
	}
}


4、在struts.xml文件中注册

		<action name="upload" class="action.UploadAction">
			<result name="input">index.jsp</result>
			<result name="success">success.jsp</result>
		</action>


到此为止就实现了整个功能。用户选择本地文件后,点击打开文件。编辑文件之后点击提交,文件会保存并提交到服务器端。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页