动态修改网页之后,如何把IHTMLDocument2中内容保存到文件? ---------------(现在无法把修改结果保存下来)

解决方案 »

  1.   

    保存到本地的话,就是用fopen等一系列函数好了。
    你不会想把你修改的页面保存到服务器上吧?如果你能做到,麻烦告诉我下。
      

  2.   

    LZ 我的意思是你怎么 IHTMLDocument2 的内容?
      

  3.   

    LZ 我的意思是你怎么获取 IHTMLDocument2 的内容?
      

  4.   

    通过CWebBrowser2控件的GetDocument函数
      

  5.   

    IHTMLElement 的get_innerhtml 得到整个body的htm代码,然后建立个文件把得到的代码写进去,就行了
      

  6.   

    思路可行,不过不是保存<body>对应的代码而是<html>对应的代码
      

  7.   

    可是如果这个网页很大的话,他的html对应的代码会很多,赋值给CString参数? 不行啊 
      

  8.   

    CHtmView::GetSource()可以试一下,看了其源码,是把IHTMLDocument2序列化到流
      

  9.   

    看了下CHtmlEditView相关的源码,就是搞动态编辑网页,然后保存的,肯定可以
    源码本机路径:C:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\src\mfc\viewhtml.cpp
    [code]
    BOOL CHtmlEditDoc::OnSaveDocument(LPCTSTR lpszFileName)
    {
    BOOL bRet = FALSE;
    CHtmlEditView *pView = GetView();
    if (pView != NULL)
    {
    CFile file;
    if (file.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite))
    {
    CArchive ar(&file, CArchive::store);
    CArchiveStream as(&ar);
    CComPtr<IHTMLDocument2> spHTMLDocument;
    CComQIPtr<IPersistStreamInit> spPSI;
    pView->GetDHtmlDocument(&spHTMLDocument);
    if (spHTMLDocument)
    {
    spPSI = spHTMLDocument;
    if (spPSI)
    {
    if (S_OK == spPSI->Save((IStream*)&as, TRUE))
    {
    SetModifiedFlag(FALSE);
    bRet = TRUE;
    }
    }
    }
    }
    }
    return bRet;
    }
    [/code]
      

  10.   


    BOOL CHtmlEditDoc::OnSaveDocument(LPCTSTR lpszFileName)
    {
    BOOL bRet = FALSE;
    CHtmlEditView *pView = GetView();
    if (pView != NULL)
    {
    CFile file;
    if (file.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite))
    {
    CArchive ar(&file, CArchive::store);
    CArchiveStream as(&ar);
    CComPtr<IHTMLDocument2> spHTMLDocument;
    CComQIPtr<IPersistStreamInit> spPSI;
    pView->GetDHtmlDocument(&spHTMLDocument);
    if (spHTMLDocument)
    {
    spPSI = spHTMLDocument;
    if (spPSI)
    {
    if (S_OK == spPSI->Save((IStream*)&as, TRUE))
    {
    SetModifiedFlag(FALSE);
    bRet = TRUE;
    }
    }
    }
    }
    }
    return bRet;
    }
      

  11.   

    下面是我的代码片段,中间有去掉对你没用的东西。这个可以保存你修改过的源码文件,但是不包括doctype对象的源码。CComQIPtr<IHTMLDocument3> pDoc3;
    if ( S_OK != pDisp2->QueryInterface(IID_IHTMLDocument3,(LPVOID*)&pDoc3) )
    {
    AfxMessageBox(TEXT("get modify source failed"));
    return;
    }
    //save modified html file
    //HTML object
    CComQIPtr<IHTMLElement> pRootElem;
    pDoc3->get_documentElement(&pRootElem);
    _bstr_t bstrHtml;
    pRootElem->get_outerHTML(bstrHtml.GetAddress());
    wstring sNewHtml( bstrHtml );

    //write to file
    strDir = strDir.substr(0, strDir.rfind(TEXT(".")));
    strDir += TEXT(".html");
    absPath = CString(arrTmpPath);
    absPath += TEXT("DevHelper\\");
    absPath += strDir.c_str();
    HANDLE hFile = CreateFile(absPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
    if ( INVALID_HANDLE_VALUE == hFile )
    {
    AfxMessageBox(TEXT("Open Save Failed"));
    return;
    }
    pDoc2->get_URL(bstrHtml.GetAddress());
    CStringA comment;
    comment.Format(("<!-- saved by DevHelper from url=%s -->\n"),CStringA(bstrHtml.GetBSTR()));
    DWORD dwBytesWritten = 0;
    BOOL bRet;
    if ( FALSE == WriteFile(hFile,comment.GetBuffer(),comment.GetLength(),&dwBytesWritten,NULL) )
    {
    AfxMessageBox(TEXT("Write temp file head failed"));
    } bRet = WriteFile(hFile,pTmp,bufLenExp,&dwBytesWritten,NULL);
    if ( FALSE == bRet )
    {
    AfxMessageBox(TEXT("Write temp file failed"));
    return;
    }
    CloseHandle(hFile);
      

  12.   

    我的开发环境是VC++ 6.0 你的代码中很多对象6.0里都没有,如IHTMLDocument3。不知道在6.0里怎么写